Skip to content
This repository was archived by the owner on Jan 30, 2021. It is now read-only.

Commit 4c9a299

Browse files
authored
Init (#1)
* Added s3 stored user data * Support s3 stored user_data * Support s3 stored user_data * Support s3 stored user_data * Address comments * Address PR comments * Address PR comments * Update main.tf
1 parent d994a7c commit 4c9a299

File tree

5 files changed

+110
-1
lines changed

5 files changed

+110
-1
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright {yyyy} {name of copyright owner}
189+
Copyright 2017 Cloud Posse LLC
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

main.tf

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Define composite variables for resources
2+
module "label" {
3+
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.2.1"
4+
namespace = "${var.namespace}"
5+
name = "${var.name}"
6+
stage = "${var.stage}"
7+
attributes = ["s3", "backend"]
8+
}
9+
10+
locals {
11+
aggregated_user_data = "${join("\n", var.user_data)}"
12+
template_path = "${path.module}/templates/${var.os}.sh"
13+
}
14+
15+
data "template_file" "default" {
16+
template = "${file(local.template_path)}"
17+
18+
vars {
19+
s3_user_data_uri = "s3://${aws_s3_bucket_object.default.bucket}${aws_s3_bucket_object.default.key}"
20+
}
21+
}
22+
23+
resource "aws_s3_bucket_object" "default" {
24+
bucket = "${var.bucket}"
25+
key = "${var.path}/user_data.sh"
26+
content = "${local.aggregated_user_data}"
27+
etag = "${md5(local.aggregated_user_data)}"
28+
}
29+
30+
## IAM Role Policy that allows access to S3
31+
resource "aws_iam_policy" "default" {
32+
name = "${module.label.id}"
33+
34+
lifecycle {
35+
create_before_destroy = true
36+
}
37+
38+
policy = "${data.aws_iam_policy_document.default.json}"
39+
}
40+
41+
data "aws_iam_policy_document" "default" {
42+
statement {
43+
actions = ["s3:ListBucket"]
44+
45+
effect = "Allow"
46+
47+
resources = [
48+
"${format("arn:aws:s3:::%v", aws_s3_bucket_object.default.bucket)}",
49+
]
50+
}
51+
52+
statement {
53+
actions = ["s3:GetObject"]
54+
55+
effect = "Allow"
56+
57+
resources = [
58+
"${format("arn:aws:s3:::%v%v", aws_s3_bucket_object.default.bucket, aws_s3_bucket_object.default.key)}",
59+
]
60+
}
61+
}

outputs.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
output "user_data" {
2+
value = "${data.template_file.default.rendered}"
3+
}
4+
5+
output "policy_arn" {
6+
value = "${aws_iam_policy.default.arn}"
7+
}

templates/ubuntu.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Install deps
2+
3+
apt-get -y install python-pip
4+
5+
# Install AWS Client
6+
pip install --upgrade awscli
7+
8+
aws s3 cp ${s3_user_data_uri} /tmp/user_data.sh
9+
10+
eval "$(cat /tmp/user_data.sh)"
11+
12+
rm -rf /tmp/user_data.sh

variables.tf

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
variable "namespace" {
2+
default = "global"
3+
}
4+
5+
variable "stage" {
6+
default = "default"
7+
}
8+
9+
variable "name" {}
10+
11+
variable "bucket" {
12+
default = "Bucket name to store user data script"
13+
}
14+
15+
variable "path" {
16+
default = "/"
17+
description = "Path to store user data script in bucket"
18+
}
19+
20+
variable "os" {
21+
default = "ubuntu"
22+
description = "Server OS that will execute user data script"
23+
}
24+
25+
variable "user_data" {
26+
type = "list"
27+
default = []
28+
description = "User data scripts content"
29+
}

0 commit comments

Comments
 (0)