Skip to content

Commit

Permalink
Merge pull request #17 from cuotos/additional_user_data
Browse files Browse the repository at this point in the history
allow the passing of additional user data
  • Loading branch information
cuotos authored Oct 30, 2022
2 parents 1f2d541 + 81c76db commit 2963990
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ The .terraform-docs.yml file contains the configuration to make sure if gets cre
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | ~> 3.0 |
| <a name="provider_random"></a> [random](#provider\_random) | n/a |
| <a name="provider_template"></a> [template](#provider\_template) | n/a |

## Modules

Expand All @@ -51,6 +52,7 @@ No modules.
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
| [aws_iam_policy_document.assume_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
| [template_file.user_data](https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/file) | data source |

## Inputs

Expand All @@ -59,6 +61,7 @@ No modules.
| <a name="input_additional_role_policies"></a> [additional\_role\_policies](#input\_additional\_role\_policies) | Additional Policies to attach to the instance in additional to SSM | `list(string)` | `[]` | no |
| <a name="input_additional_security_groups"></a> [additional\_security\_groups](#input\_additional\_security\_groups) | Addition security groups to assign to the instance | `list(string)` | `[]` | no |
| <a name="input_additional_tags"></a> [additional\_tags](#input\_additional\_tags) | Map of tags to add to all resources | `map(string)` | `{}` | no |
| <a name="input_additional_user_data"></a> [additional\_user\_data](#input\_additional\_user\_data) | Additional code to be added to the user\_data.sh script | `string` | `null` | no |
| <a name="input_ami"></a> [ami](#input\_ami) | Specify an AMI to run, if not it will use the latest Amazon Linux, or Windows Server image. | `string` | `""` | no |
| <a name="input_associate_public_ip_address"></a> [associate\_public\_ip\_address](#input\_associate\_public\_ip\_address) | Assign public IP to the instance. | `bool` | `true` | no |
| <a name="input_comment"></a> [comment](#input\_comment) | Comment tag to add to all resources | `string` | `""` | no |
Expand Down
9 changes: 8 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,19 @@ resource "aws_iam_instance_profile" "profile" {
role = aws_iam_role.role.name
}

data "template_file" "user_data" {
template = file("${path.module}/user_data.sh")
vars = {
additional_user_data = var.additional_user_data != null ? var.additional_user_data : ""
}
}

# Create the EC2 compute server
resource "aws_instance" "instance" {
key_name = var.key_name != "" ? var.key_name : null
instance_type = var.instance_type
ami = var.ami != "" ? var.ami : data.aws_ami.this.image_id
user_data = filebase64("${path.module}/user_data.sh")
user_data = data.template_file.user_data.rendered
iam_instance_profile = aws_iam_instance_profile.profile.id
subnet_id = var.subnet_id
associate_public_ip_address = var.associate_public_ip_address
Expand Down
4 changes: 3 additions & 1 deletion user_data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ chmod +x /usr/local/bin/docker-compose

# Login to ECR
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
aws ecr get-login-password --region eu-west-1 | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.eu-west-1.amazonaws.com
aws ecr get-login-password --region eu-west-1 | docker login --username AWS --password-stdin $${AWS_ACCOUNT_ID}.dkr.ecr.eu-west-1.amazonaws.com

${additional_user_data}
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,10 @@ variable "additional_tags" {
type = map(string)
description = "Map of tags to add to all resources"
default = {}
}

variable "additional_user_data" {
type = string
description = "Additional code to be added to the user_data.sh script"
default = null
}

0 comments on commit 2963990

Please sign in to comment.