Skip to content

Commit 55bf294

Browse files
author
Bhavik Kumar
authored
Improve tagging of AWS resources (#63)
* Add git author and repo slug to tags * Upgrade to latest Terraform ECS service module which supports tagging * Remove host port so ports are allocated dynamically * Remove task level CPU and Memory limits when using EC2 launch type
1 parent f279707 commit 55bf294

File tree

5 files changed

+14
-17
lines changed

5 files changed

+14
-17
lines changed

.travis.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ before_install:
3535
- chmod +x $HOME/bin/terraform
3636
- export AWS_ACCESS_KEY_ID=$DEPLOYMENT_ACCESS_KEY_ID
3737
- export AWS_SECRET_ACCESS_KEY=$DEPLOYMENT_SECRET_ACCESS_KEY
38+
- export AUTHOR_NAME="$(git log -1 $TRAVIS_COMMIT --pretty="%aN")"
3839
before_cache:
3940
- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
4041
- rm -fr $HOME/.gradle/caches/*/plugin-resolution/
@@ -54,7 +55,7 @@ jobs:
5455
script:
5556
- cd deployment/terraform/ecr
5657
- terraform init -backend-config="bucket=${STATE_S3_BUCKET}" -backend-config="region=${AWS_DEFAULT_REGION}" -backend-config="dynamodb_table=${STATE_DYNAMODB_TABLE}" -backend-config="kms_key_id=${KMS_KEY_ID}" -backend-config="key=${KEY}" -backend-config="role_arn=${ROLE_ARN}" 1>/dev/null || exit 1
57-
- terraform apply -backup="-" -input=false -auto-approve -var role_arn=${ROLE_ARN} -var service_name=${SERVICE_NAME} 1>/dev/null || exit 1
58+
- terraform apply -backup="-" -input=false -auto-approve -var role_arn=${ROLE_ARN} -var service_name=${SERVICE_NAME} -var tags="{\"AuthorName\":\"${AUTHOR_NAME}\",\"GitRepository\":\"${TRAVIS_REPO_SLUG}\"}" 1>/dev/null || exit 1
5859
- REPOSITORY_URI=$(terraform output repository_url)
5960
- cd $TRAVIS_BUILD_DIR
6061
- eval $(aws sts assume-role --role-arn "$OPERATIONS_ROLE_ARN" --role-session-name "${TRAVIS_REPO_SLUG//\//-}" | jq -r '.Credentials | @sh "export AWS_SESSION_TOKEN=\(.SessionToken)\nexport AWS_ACCESS_KEY_ID=\(.AccessKeyId)\nexport AWS_SECRET_ACCESS_KEY=\(.SecretAccessKey) "')
@@ -63,7 +64,6 @@ jobs:
6364
after_success:
6465
- ./gradlew sonarqube
6566
- stage: deploy to development
66-
jdk: openjdk11
6767
env:
6868
- TF_WORKSPACE=development
6969
- SPLUNK_TOKEN=
@@ -78,7 +78,6 @@ jobs:
7878
all_branches: true
7979
- stage: deploy to production
8080
if: env(DEPLOY_PRODUCTION) IS present
81-
jdk: openjdk11
8281
env:
8382
- TF_WORKSPACE=production
8483
- SPLUNK_TOKEN=

deployment/script/travis_deploy.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
cd $TRAVIS_BUILD_DIR
44
cd deployment/terraform/ecs-service
55
terraform init -backend-config="bucket=${STATE_S3_BUCKET}" -backend-config="region=${AWS_DEFAULT_REGION}" -backend-config="dynamodb_table=${STATE_DYNAMODB_TABLE}" -backend-config="kms_key_id=${KMS_KEY_ID}" -backend-config="key=${SERVICE_KEY}" -backend-config="role_arn=${ROLE_ARN}" 1> /dev/null || exit 1
6-
terraform apply -backup="-" -input=false -auto-approve -var role_arn=${ROLE_ARN} -var service_name=${SERVICE_NAME} -var service_version=${TRAVIS_BUILD_NUMBER} -var splunk_url=${SPLUNK_URL} -var splunk_token=${SPLUNK_TOKEN} 1> /dev/null || exit 1
6+
terraform apply -backup="-" -input=false -auto-approve -var role_arn=${ROLE_ARN} -var service_name=${SERVICE_NAME} -var service_version=${TRAVIS_BUILD_NUMBER} -var splunk_url=${SPLUNK_URL} -var splunk_token=${SPLUNK_TOKEN} -var tags="{\"AuthorName\":\"${AUTHOR_NAME}\",\"GitRepository\":\"${TRAVIS_REPO_SLUG}\"}" 1> /dev/null || exit 1
77
eval $(terraform output -json | jq -r .' | @sh "export CLUSTER_NAME=\(.ecs_cluster_name.value)\nexport DEPLOYMENT_ROLE_ARN=\(.deployment_role_arn.value) "')
88
eval $(aws sts assume-role --role-arn "$DEPLOYMENT_ROLE_ARN" --role-session-name "${TRAVIS_REPO_SLUG//\//-}" | jq -r '.Credentials | @sh "export AWS_SESSION_TOKEN=\(.SessionToken)\nexport AWS_ACCESS_KEY_ID=\(.AccessKeyId)\nexport AWS_SECRET_ACCESS_KEY=\(.SecretAccessKey) "')
99
aws ecs wait services-stable --services ${SERVICE_NAME} --cluster ${CLUSTER_NAME} || exit 1

deployment/terraform/ecr/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ locals {
1515

1616
provider "aws" {
1717
region = var.aws_default_region
18-
version = "~> 2.36.0"
18+
version = "~> 2.38.0"
1919
profile = var.profile
2020

2121
allowed_account_ids = [

deployment/terraform/ecs-service/ecs.tf

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ module "container_definition" {
5555
portMappings = [
5656
{
5757
containerPort = 8080
58-
hostPort = 8080,
58+
hostPort = 0,
5959
protocol = "tcp"
6060
}
6161
]
@@ -64,7 +64,7 @@ module "container_definition" {
6464
resource "aws_iam_role" "execution_task_role" {
6565
name = format("%s-execution", var.service_name)
6666
assume_role_policy = data.aws_iam_policy_document.task_service_assume_role.json
67-
tags = var.tags
67+
tags = merge(local.common_tags, var.tags)
6868
}
6969

7070
resource "aws_iam_role_policy_attachment" "ecs_task_default_policy" {
@@ -75,21 +75,19 @@ resource "aws_iam_role_policy_attachment" "ecs_task_default_policy" {
7575
resource "aws_iam_role" "task_role" {
7676
name = format("%s-task", var.service_name)
7777
assume_role_policy = data.aws_iam_policy_document.task_service_assume_role.json
78-
tags = var.tags
78+
tags = merge(local.common_tags, var.tags)
7979
}
8080

8181
resource "aws_ecs_task_definition" "task_definition" {
8282
container_definitions = "[${module.container_definition.container_definition}]"
8383
family = var.service_name
84-
cpu = var.cpu
85-
memory = var.memory
8684
execution_role_arn = aws_iam_role.execution_task_role.arn
8785
task_role_arn = aws_iam_role.task_role.arn
8886
tags = merge(local.common_tags, var.tags)
8987
}
9088

9189
module "ecs_service" {
92-
source = "git::https://github.com/bnc-projects/terraform-ecs-service.git?ref=1.3.2"
90+
source = "git::https://github.com/bnc-projects/terraform-ecs-service.git?ref=1.3.4"
9391
application_path = "/v1/sbjb"
9492
attach_load_balancer = true
9593
cluster = data.terraform_remote_state.market_data.outputs.ecs_cluster_name

deployment/terraform/ecs-service/variables.tf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ variable "role_arn" {
3131
description = "The role to assume to access the terraform remote state"
3232
}
3333

34-
variable "tags" {
35-
type = map(string)
36-
description = "A map of tags to add to all resources"
37-
default = {}
38-
}
39-
4034
variable "service_name" {
4135
type = string
4236
description = "The name of the ECS service"
@@ -56,3 +50,9 @@ variable "splunk_token" {
5650
type = string
5751
description = "The token used to send log to Splunk collector"
5852
}
53+
54+
variable "tags" {
55+
type = map(string)
56+
description = "A map of tags to add to all resources"
57+
default = {}
58+
}

0 commit comments

Comments
 (0)