Skip to content

Commit

Permalink
Store user_data scripts in S3.
Browse files Browse the repository at this point in the history
  • Loading branch information
dumptruckman committed Mar 1, 2024
1 parent b330b28 commit 4901a8f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
32 changes: 30 additions & 2 deletions asg.tf
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,21 @@ resource "aws_launch_template" "web_template" {
var.utility_accessible_sg,
]

user_data = base64encode(templatefile("${path.module}/templates/web_user_data.sh", local.web_interpolation_vars))
user_data = <<EOF
#-------------------------------------------#
#-----> COPY THE CONFIG FILES FROM S3 <-----#
#-------------------------------------------#
sudo aws s3 cp s3://${var.user_data_bucket_name}/web_user_data.sh ./
sudo chmod +x web_user_data.sh
#-------------------------------------------#
#---------> RUN THE CONFIG FILES <---------#
#-------------------------------------------#
./web_user_data.sh
EOF

iam_instance_profile {
name = aws_iam_instance_profile.concourse_profile.name
Expand Down Expand Up @@ -123,7 +137,21 @@ resource "aws_launch_template" "worker_template" {
aws_security_group.worker_sg.id,
]

user_data = base64encode(templatefile("${path.module}/templates/worker_user_data.sh", local.worker_interpolation_vars))
user_data = <<EOF
#-------------------------------------------#
#-----> COPY THE CONFIG FILES FROM S3 <-----#
#-------------------------------------------#
sudo aws s3 cp s3://${var.user_data_bucket_name}/worker_user_data.sh ./
sudo chmod +x worker_user_data.sh
#-------------------------------------------#
#---------> RUN THE CONFIG FILES <---------#
#-------------------------------------------#
./worker_user_data.sh
EOF

iam_instance_profile {
name = aws_iam_instance_profile.concourse_profile.name
Expand Down
7 changes: 4 additions & 3 deletions iam.tf
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,16 @@ resource "aws_iam_role_policy_attachment" "cloudwatch_policies" {
policy_arn = data.aws_iam_policy.cloudwatch_agent_policy.arn
}

resource "aws_iam_role_policy" "ssm_get_parameters" {
name = "ConcourseCI-SSM-GetParameters"
resource "aws_iam_role_policy" "s3_get_user_data" {
name = "ConcourseCI-S3-Retrieve-UserData"
role = aws_iam_role.concourse_role.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"ssm:GetParameter"
"s3:ListBucket",
"s3:GetObject"
]
Effect = "Allow"
Resource = "*"
Expand Down
20 changes: 20 additions & 0 deletions s3.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
resource "aws_s3_bucket" "user_data" {
bucket = var.user_data_bucket_name
}

resource "aws_s3_bucket_acl" "user_data_acl" {
bucket = aws_s3_bucket.user_data.id
acl = "private"
}

resource "aws_s3_bucket_object" "web_user_data" {
bucket = aws_s3_bucket.user_data.id
key = "web_user_data.sh"
content = templatefile("${path.module}/templates/web_user_data.sh", local.web_interpolation_vars)
}

resource "aws_s3_bucket_object" "worker_user_data" {
bucket = aws_s3_bucket.user_data.id
key = "worker_user_data.sh"
content = templatefile("${path.module}/templates/worker_user_data.sh", local.worker_interpolation_vars)
}
5 changes: 5 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,8 @@ variable "cloudwatch_namespace" {
default = "Concourse"
description = "The namespace to use for CloudWatch metrics."
}

variable "user_data_bucket_name" {
default = "concourse-user-data"
description = "The name of the S3 bucket to store user data (init scripts) in."
}

0 comments on commit 4901a8f

Please sign in to comment.