Skip to content
This repository was archived by the owner on Jan 30, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Define composite variables for resources
module "label" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.2.1"
namespace = "${var.namespace}"
name = "${var.name}"
stage = "${var.stage}"
attributes = ["s3", "stored"]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/stored/user-data/

Copy link

@osterman osterman Oct 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop s3 since I don't think we need that in the bucket name (it's implied)

Aha, I see. I thought it would be used for the bucket name, but it's used for a policy. We can keep it.

}

data "template_file" "default" {
template = "${file("${path.module}/user_data.sh")}"

vars {
s3_path = "${aws_s3_bucket_object.default.bucket}${aws_s3_bucket_object.default.key}"
Copy link

@osterman osterman Oct 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/s3_path/s3_user_data_uri/ and add s3:// to the uri

}
}

resource "aws_s3_bucket_object" "default" {
bucket = "${var.bucket}"
key = "${var.path}/user_data.sh"
content = "${join("\n", var.user_data)}"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a local value for the join output?

etag = "${md5(join("\n", var.user_data))}"
}

## IAM Role Policy that allows access to S3
resource "aws_iam_policy" "default" {
name = "${module.label.id}"

lifecycle {
create_before_destroy = true
}

policy = "${data.aws_iam_policy_document.default.json}"
}

data "aws_iam_policy_document" "default" {
statement {
actions = [
"s3:ListAllMyBuckets",
]

effect = "Allow"

resources = [
"arn:aws:s3:::*",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be necessary and is too permissive.

]
}

statement {
actions = [ "s3:ListBucket" ]

effect = "Allow"

resources = [
"${format("arn:aws:s3:::%v", aws_s3_bucket_object.default.bucket)}"
]
}

statement {
actions = [ "s3:*" ]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The instances should only need to be able to retrieve the object, not manipulate it.


effect = "Allow"

resources = [
"${format("arn:aws:s3:::%v%v", aws_s3_bucket_object.default.bucket, aws_s3_bucket_object.default.key)}"
]
}
}
7 changes: 7 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
output "user_data" {
value = "${data.template_file.default.rendered}"
}

output "policy_arn" {
value = "${aws_iam_policy.default.arn}"
}
15 changes: 15 additions & 0 deletions user_data.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

##############
# Install deps
##############
Copy link

@osterman osterman Oct 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This preamble is disproportionately large to the other comments and there is no "main" section. I think you can drop the #######


apt-get -y install python-pip

# Install AWS Client
pip install --upgrade awscli

aws s3 cp s3://${s3_path} /tmp/user_data.sh
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop the s3:// and use s3_user_data_uri


eval "$(cat /tmp/user_data.sh)"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why eval as opposed to just running it? .... eval limits user data to bash.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because user data does not contains
'#/bin/bash' to specifiy shell


rm -rf /tmp/user_data.sh
20 changes: 20 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
variable "namespace" {
default = "global"
}

variable "stage" {
default = "default"
}

variable "name" {}

variable "bucket" {}

variable "path" {
default = "/"
}

variable "user_data" {
type = "list"
default = []
}