Skip to content

Commit a4db7d2

Browse files
authored
Merge pull request #49 from oozou/terraform-test
chore: add terraform test
2 parents 70d6506 + 66a34c1 commit a4db7d2

17 files changed

+2022
-22
lines changed

.github/workflows/code-scan.yml

Lines changed: 0 additions & 21 deletions
This file was deleted.

.github/workflows/terraform-test.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Test Module
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- '*.tf'
7+
- 'tests/**'
8+
- 'examples/terraform-test/**'
9+
- '.github/workflows/terraform-test.yaml'
10+
workflow_dispatch:
11+
12+
permissions:
13+
contents: read
14+
pull-requests: write
15+
id-token: write
16+
17+
jobs:
18+
test:
19+
name: Run Terraform Tests
20+
uses: oozou/.github/.github/workflows/terraform-test.yml@main
21+
secrets: inherit
22+
with:
23+
aws_region: 'ap-southeast-1'
24+
tf_version: '1.9.8'
25+
go_version: '1.21'
26+
test_example_path: 'examples/terraform-test'
27+
timeout_minutes: 60
28+
module_name: 'AWS ECS Service'
29+
iam_oidc_role: 'arn:aws:iam::562563527952:role/oozou-internal-devops-github-action-oidc-role' # oozou internal account

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ example/
1818
crash.log
1919
crash.*.log
2020

21+
# report
22+
tests/test-report*
23+
2124
# Exclude all .tfvars files, which are likely to contain sensitive data, such as
2225
# password, private keys, and other secrets. These should not be part of version
2326
# control as they are data points which are potentially sensitive and subject

examples/terraform-test/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!-- BEGIN_TF_DOCS -->
2+
## Requirements
3+
4+
| Name | Version |
5+
|------|---------|
6+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0.0 |
7+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.0.0, < 5.0.0 |
8+
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2.3.0 |
9+
10+
## Providers
11+
12+
| Name | Version |
13+
|------|---------|
14+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 4.67.0 |
15+
16+
## Modules
17+
18+
| Name | Source | Version |
19+
|------|--------|---------|
20+
| <a name="module_api_service"></a> [api\_service](#module\_api\_service) | ../.. | n/a |
21+
| <a name="module_fargate_cluster"></a> [fargate\_cluster](#module\_fargate\_cluster) | oozou/ecs-fargate-cluster/aws | 1.0.7 |
22+
| <a name="module_payment_service"></a> [payment\_service](#module\_payment\_service) | ../.. | n/a |
23+
| <a name="module_vpc"></a> [vpc](#module\_vpc) | oozou/vpc/aws | 1.2.4 |
24+
25+
## Resources
26+
27+
| Name | Type |
28+
|------|------|
29+
| [aws_caller_identity.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
30+
31+
## Inputs
32+
33+
| Name | Description | Type | Default | Required |
34+
|------|-------------|------|---------|:--------:|
35+
| <a name="input_custom_tags"></a> [custom\_tags](#input\_custom\_tags) | Custom tags which can be passed on to the AWS resources. They should be key value pairs having distinct keys. | `map(string)` | `{}` | no |
36+
| <a name="input_environment"></a> [environment](#input\_environment) | [Required] Name prefix used for resource naming in this component | `string` | n/a | yes |
37+
| <a name="input_name"></a> [name](#input\_name) | [Required] Name of Platfrom or application | `string` | n/a | yes |
38+
| <a name="input_prefix"></a> [prefix](#input\_prefix) | [Required] Name prefix used for resource naming in this component | `string` | n/a | yes |
39+
40+
## Outputs
41+
42+
No outputs.
43+
<!-- END_TF_DOCS -->

examples/terraform-test/main.tf

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/* -------------------------------------------------------------------------- */
2+
/* Data */
3+
/* -------------------------------------------------------------------------- */
4+
data "aws_caller_identity" "this" {}
5+
6+
data "aws_availability_zones" "available" {
7+
state = "available"
8+
}
9+
10+
/* -------------------------------------------------------------------------- */
11+
/* VPC */
12+
/* -------------------------------------------------------------------------- */
13+
module "vpc" {
14+
source = "oozou/vpc/aws"
15+
version = "1.2.5"
16+
prefix = var.prefix
17+
environment = var.environment
18+
account_mode = "spoke"
19+
20+
cidr = "10.0.0.0/16"
21+
public_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
22+
private_subnets = ["10.0.3.0/24", "10.0.4.0/24"]
23+
availability_zone = slice(data.aws_availability_zones.available.names, 0, 2)
24+
25+
is_create_nat_gateway = true
26+
is_enable_single_nat_gateway = true
27+
is_enable_dns_hostnames = true
28+
is_enable_dns_support = true
29+
is_create_flow_log = false
30+
is_enable_flow_log_s3_integration = false
31+
32+
tags = var.custom_tags
33+
}
34+
35+
/* -------------------------------------------------------------------------- */
36+
/* ACM */
37+
/* -------------------------------------------------------------------------- */
38+
module "acm" {
39+
source = "oozou/acm/aws"
40+
version = "1.0.4"
41+
42+
acms_domain_name = {
43+
cms = {
44+
domain_name = "terraform-test.devops.team.oozou.com"
45+
}
46+
}
47+
route53_zone_name = "devops.team.oozou.com"
48+
is_automatic_verify_acms = true
49+
}
50+
51+
52+
/* -------------------------------------------------------------------------- */
53+
/* Fargate Cluster */
54+
/* -------------------------------------------------------------------------- */
55+
module "fargate_cluster" {
56+
source = "oozou/ecs-fargate-cluster/aws"
57+
version = "1.1.0"
58+
# Generics
59+
prefix = var.prefix
60+
environment = var.environment
61+
name = var.name
62+
63+
# IAM Role
64+
## If is_create_role is false, all of folowing argument is ignored
65+
is_create_role = true
66+
allow_access_from_principals = ["arn:aws:iam::${data.aws_caller_identity.this.account_id}:root"]
67+
additional_managed_policy_arns = []
68+
69+
# VPC Information
70+
vpc_id = module.vpc.vpc_id
71+
72+
# ALB
73+
is_create_alb = true
74+
is_public_alb = true
75+
enable_deletion_protection = false
76+
alb_listener_port = 443
77+
is_ignore_unsecured_connection = true
78+
public_subnet_ids = module.vpc.public_subnet_ids
79+
is_create_alb_dns_record = true
80+
alb_certificate_arn = module.acm.certificate_arns["cms"]
81+
route53_hosted_zone_name = "devops.team.oozou.com"
82+
fully_qualified_domain_name = "terraform-test.devops.team.oozou.com"
83+
84+
85+
tags = var.custom_tags
86+
}
87+
88+
/* -------------------------------------------------------------------------- */
89+
/* Service */
90+
/* -------------------------------------------------------------------------- */
91+
module "api_service" {
92+
source = "../.."
93+
94+
prefix = var.prefix
95+
environment = var.environment
96+
name = format("%s-api-service", var.name)
97+
98+
# ECS service
99+
task_cpu = 1024
100+
task_memory = 2048
101+
ecs_cluster_name = module.fargate_cluster.ecs_cluster_name
102+
service_discovery_namespace = module.fargate_cluster.service_discovery_namespace
103+
is_enable_execute_command = true
104+
application_subnet_ids = module.vpc.private_subnet_ids
105+
security_groups = [
106+
module.fargate_cluster.ecs_task_security_group_id
107+
]
108+
additional_ecs_task_role_policy_arns = [
109+
"arn:aws:iam::aws:policy/AmazonSSMFullAccess"
110+
]
111+
112+
# ALB
113+
alb_listener_arn = module.fargate_cluster.alb_listener_http_arn
114+
alb_host_header = null
115+
alb_paths = ["/*"]
116+
alb_priority = "100"
117+
vpc_id = module.vpc.vpc_id
118+
health_check = {
119+
interval = 20,
120+
path = "/",
121+
timeout = 10,
122+
healthy_threshold = 3,
123+
unhealthy_threshold = 3,
124+
matcher = "200,201,204"
125+
}
126+
127+
is_create_cloudwatch_log_group = true
128+
129+
container = {
130+
main_container = {
131+
name = format("%s-%s-%s-api-service", var.prefix, var.environment, var.name)
132+
image = "nginx"
133+
cpu = 128
134+
memory = 256
135+
is_attach_to_lb = true
136+
port_mappings = [
137+
{
138+
# If a container has multiple ports, index 0 will be used for target group
139+
host_port = 80
140+
container_port = 80
141+
protocol = "tcp"
142+
}
143+
]
144+
entry_point = []
145+
command = []
146+
}
147+
}
148+
environment_variables = {
149+
main_container = {
150+
THIS_IS_ENV = "ENV1",
151+
THIS_IS_ENVV = "ENVV",
152+
}
153+
side_container = {
154+
XXXX = "XXXX",
155+
XXXXX = "XXXXX",
156+
}
157+
}
158+
secret_variables = {
159+
main_container = {
160+
THIS_IS_SECRET = "1xxxxx",
161+
THIS_IS_SECRETT = "2xxxxx",
162+
}
163+
}
164+
165+
target_tracking_configuration = {
166+
policy_type = "TargetTrackingScaling"
167+
name = "cpu-average"
168+
capacity = {
169+
min_capacity = 1
170+
max_capacity = 10
171+
}
172+
scaling_behaviors = {
173+
predefined_metric_type = "ECSServiceAverageCPUUtilization"
174+
target_value = 60
175+
scale_in_cooldown = 180
176+
scale_out_cooldown = 60
177+
}
178+
}
179+
180+
tags = var.custom_tags
181+
}

examples/terraform-test/outputs.tf

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# VPC outputs
2+
output "vpc_id" {
3+
description = "VPC ID"
4+
value = module.vpc.vpc_id
5+
}
6+
7+
# ECS Cluster outputs
8+
output "ecs_cluster_name" {
9+
description = "ECS Cluster name"
10+
value = module.fargate_cluster.ecs_cluster_name
11+
}
12+
13+
output "ecs_cluster_arn" {
14+
description = "ECS Cluster ARN"
15+
value = module.fargate_cluster.ecs_cluster_arn
16+
}
17+
18+
output "alb_dns_name" {
19+
description = "ALB DNS name"
20+
value = module.fargate_cluster.alb_dns_name
21+
}
22+
23+
output "alb_arn" {
24+
description = "ALB ARN"
25+
value = module.fargate_cluster.alb_arn
26+
}
27+
28+
output "alb_listener_http_arn" {
29+
description = "ALB HTTP listener ARN"
30+
value = module.fargate_cluster.alb_listener_http_arn
31+
}
32+
33+
# ECS Service outputs
34+
output "service_name" {
35+
description = "ECS Service name"
36+
value = module.api_service.service_name
37+
}
38+
39+
output "service_arn" {
40+
description = "ECS Service ARN"
41+
value = module.api_service.service_arn
42+
}
43+
44+
output "task_definition_arn" {
45+
description = "ECS Task Definition ARN"
46+
value = module.api_service.task_definition_arn
47+
}
48+
49+
output "target_group_arn" {
50+
description = "Target Group ARN"
51+
value = module.api_service.target_group_arn
52+
}
53+
54+
output "target_group_id" {
55+
description = "Target Group ID"
56+
value = module.api_service.target_group_id
57+
}
58+
59+
output "task_role_arn" {
60+
description = "ECS Task role ARN"
61+
value = module.api_service.task_role_arn
62+
}
63+
64+
output "task_execution_role_arn" {
65+
description = "ECS Task execution role ARN"
66+
value = module.api_service.task_execution_role_arn
67+
}
68+
69+
output "cloudwatch_log_group_name" {
70+
description = "CloudWatch log group name"
71+
value = module.api_service.cloudwatch_log_group_name
72+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
prefix = "oozou"
2+
environment = "devops"
3+
name = "demo"
4+
custom_tags = {
5+
"Remark" = "terraform-aws-ecs-service"
6+
}

examples/terraform-test/variables.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
variable "name" {
2+
description = "[Required] Name of Platfrom or application"
3+
type = string
4+
}
5+
6+
variable "prefix" {
7+
description = "[Required] Name prefix used for resource naming in this component"
8+
type = string
9+
}
10+
11+
variable "environment" {
12+
description = "[Required] Name prefix used for resource naming in this component"
13+
type = string
14+
}
15+
16+
variable "custom_tags" {
17+
description = "Custom tags which can be passed on to the AWS resources. They should be key value pairs having distinct keys."
18+
type = map(string)
19+
default = {}
20+
}

0 commit comments

Comments
 (0)