Skip to content

Commit 0f07036

Browse files
authored
Add tag support to ecs-service (techservicesillinois#48)
* Add tag support to ecs-service * Replace Drone with GitHub Actions
1 parent dec26a4 commit 0f07036

File tree

8 files changed

+111
-10
lines changed

8 files changed

+111
-10
lines changed

Diff for: .github/workflows/terraform.yml

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Some of this logic was stolen from https://www.freecodecamp.org/news/a-lightweight-tool-agnostic-ci-cd-flow-with-github-actions/
2+
3+
name: terraform
4+
5+
on:
6+
push:
7+
# branches:
8+
# - master
9+
pull_request:
10+
11+
jobs:
12+
terraform:
13+
name: terraform
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: checkout
17+
uses: actions/checkout@v2
18+
with:
19+
fetch-depth: 1
20+
21+
- name: terraform setup
22+
uses: hashicorp/setup-terraform@v1
23+
with:
24+
terraform_version: 0.12.29
25+
# cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
26+
27+
# TODO: This step duplicates work done by the Makefile.
28+
# - name: check terraform formatting
29+
# id: fmt
30+
# run: |
31+
# terraform fmt -check -recursive
32+
33+
- name: run make
34+
# env:
35+
# TOKEN: ${{ secrets.TOKEN }}
36+
run: |
37+
make all
38+
39+
# - name: terraform init
40+
# id: init
41+
# run: terraform init
42+
#
43+
# - name: terraform plan
44+
# id: plan
45+
# if: github.event_name == 'pull_request'
46+
# run: terraform plan -no-color
47+
# continue-on-error: true
48+
#
49+
# - uses: actions/[email protected]
50+
# if: github.event_name == 'pull_request'
51+
# env:
52+
# PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
53+
# with:
54+
# github-token: ${{ secrets.GITHUB_TOKEN }}
55+
# script: |
56+
# const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\`
57+
# #### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
58+
# #### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
59+
#
60+
# <details><summary>Show Plan</summary>
61+
#
62+
# \`\`\`${process.env.PLAN}\`\`\`
63+
#
64+
# </details>
65+
#
66+
# *Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
67+
#
68+
#
69+
# github.issues.createComment({
70+
# issue_number: context.issue.number,
71+
# owner: context.repo.owner,
72+
# repo: context.repo.repo,
73+
# body: output
74+
# })
75+
#
76+
# - name: terraform plan status
77+
# if: steps.plan.outcome == 'failure'
78+
# run: exit 1
79+
#
80+
# - name: terraform apply
81+
# if: github.ref == 'refs/heads/master' && github.event_name == 'push'
82+
# run: terraform apply -auto-approve

Diff for: Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM hashicorp/terraform
1+
FROM hashicorp/terraform:0.12
22

33
RUN apk add make
44

Diff for: Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ test:
3838
# Do NOT define a variable in files other than variables.tf
3939
! $(EGREP) 'variable\s+"\w+"\s*\{' $(SRCS) --exclude=variables.tf
4040
# DO put a badge in top-level README.md
41-
grep -q "\[\!\[Build Status\]([^)]*$(REPO)/status.svg)\]([^)]*$(REPO))" README.md
41+
grep -q "\[\!\[Terraform actions status\]([^)]*$(REPO)/workflows/terraform/badge.svg)\]([^)]*$(REPO)/actions)" README.md
4242
# Do NOT split a source line over more than one line
4343
! $(GREP) 'source\s*=\s*$$' $(SRCS) $(DOCS)
4444
# Do NOT use ?ref= in source lines in a README.md!

Diff for: README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# ecs-service
22

3-
[![Build Status](https://drone.techservices.illinois.edu/api/badges/techservicesillinois/terraform-aws-ecs-service/status.svg)](https://drone.techservices.illinois.edu/techservicesillinois/terraform-aws-ecs-service)
3+
[![Terraform actions status](https://github.com/techservicesillinois/terraform-aws-ecs-service/workflows/terraform/badge.svg)](https://github.com/techservicesillinois/terraform-aws-ecs-service/actions)
44

55
Provides an ECS service - effectively a task that is expected to
66
run until an error occurs or a user terminates it (typically a
@@ -275,6 +275,8 @@ that points to the load balancer. Requires that a `load_balancer` block is defin
275275

276276
* `autoscale` – (Optional) An [autoscale](#autoscale) block used to create an autoscaling configuration. Requires that a `autoscale` block is defined. Learn more about [ECS autoscaling](https://docs.aws.amazon.com/autoscaling/application/userguide/application-auto-scaling-step-scaling-policies.html).
277277

278+
* `tags` - Tags to be applied to resources where supported.
279+
278280
`autoscale`
279281
----------
280282

Diff for: main.tf

+16
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ resource "aws_ecs_service" "awsvpc_all" {
6262
0,
6363
)
6464
}
65+
66+
tags = merge({ "Name" = var.name }, var.tags)
6567
}
6668

6769
# This resource is conditionally built when using awsvpc network mode
@@ -109,6 +111,8 @@ resource "aws_ecs_service" "awsvpc_lb" {
109111
security_groups = local.security_groups
110112
subnets = local.all_subnets
111113
}
114+
115+
tags = merge({ "Name" = var.name }, var.tags)
112116
}
113117

114118
# This resource is conditionally built when using awsvpc network mode
@@ -162,6 +166,8 @@ resource "aws_ecs_service" "awsvpc_sd" {
162166
0,
163167
)
164168
}
169+
170+
tags = merge({ "Name" = var.name }, var.tags)
165171
}
166172

167173
# This resource is conditionally built when using awsvpc network
@@ -203,6 +209,8 @@ resource "aws_ecs_service" "awsvpc" {
203209
security_groups = local.security_groups
204210
subnets = local.all_subnets
205211
}
212+
213+
tags = merge({ "Name" = var.name }, var.tags)
206214
}
207215

208216
# This resource is conditionally built when not using awsvpc network
@@ -257,6 +265,8 @@ resource "aws_ecs_service" "all" {
257265
0,
258266
)
259267
}
268+
269+
tags = merge({ "Name" = var.name }, var.tags)
260270
}
261271

262272
# This resource is conditionally built when not using awsvpc network
@@ -299,6 +309,8 @@ resource "aws_ecs_service" "lb" {
299309
container_port = local.container_port
300310
target_group_arn = aws_lb_target_group.default[0].arn
301311
}
312+
313+
tags = merge({ "Name" = var.name }, var.tags)
302314
}
303315

304316
# This resource is conditionally built when not using awsvpc network mode
@@ -347,6 +359,8 @@ resource "aws_ecs_service" "sd" {
347359
0,
348360
)
349361
}
362+
363+
tags = merge({ "Name" = var.name }, var.tags)
350364
}
351365

352366
# This resource is conditionally built when not using awsvpc network
@@ -385,4 +399,6 @@ resource "aws_ecs_service" "default" {
385399
}
386400

387401
platform_version = var.platform_version
402+
403+
tags = merge({ "Name" = var.name }, var.tags)
388404
}

Diff for: sg.tf

+2-6
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,8 @@ resource "aws_security_group" "default" {
2525
description = "security group for ${var.name} service"
2626
name = var.name
2727
vpc_id = data.aws_subnet.selected[0].vpc_id
28-
tags = merge(
29-
{
30-
"Name" = var.name
31-
},
32-
var.tags,
33-
)
28+
29+
tags = merge({ "Name" = var.name }, var.tags)
3430
}
3531

3632
# Allow the containers to receive packets from the LB

Diff for: target_group.tf

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,6 @@ resource "aws_lb_target_group" "default" {
4242
}
4343
}
4444
target_type = local.network_mode == "awsvpc" ? "ip" : "instance"
45-
tags = var.tags
45+
46+
tags = merge({ "Name" = var.name }, var.tags)
4647
}

Diff for: task.tf

+4
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ resource "aws_ecs_task_definition" "fargate" {
6464
cpu = local.cpu
6565
memory = local.memory
6666
requires_compatibilities = ["FARGATE"]
67+
68+
tags = merge({ "Name" = var.name }, var.tags)
6769
}
6870

6971
resource "aws_ecs_task_definition" "ec2" {
@@ -106,4 +108,6 @@ resource "aws_ecs_task_definition" "ec2" {
106108
}
107109

108110
requires_compatibilities = ["EC2"]
111+
112+
tags = merge({ "Name" = var.name }, var.tags)
109113
}

0 commit comments

Comments
 (0)