Skip to content

Commit e7d2e79

Browse files
authored
Merge pull request #5 from sourcefuse/feature/arc-dms-terratest
test(terraform): add Terratest setup using Go
2 parents 48027d6 + c46bce1 commit e7d2e79

File tree

7 files changed

+112
-9
lines changed

7 files changed

+112
-9
lines changed

.github/workflows/terraform-test.yaml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
name: Terratest
3+
on: # yamllint disable-line rule:truthy
4+
pull_request:
5+
types: [opened]
6+
workflow_dispatch:
7+
inputs:
8+
pr_number:
9+
description: 'Pull Request Number'
10+
required: true
11+
12+
permissions:
13+
id-token: write
14+
contents: read
15+
statuses: write # Required for setting commit status
16+
17+
jobs:
18+
terratest:
19+
runs-on: ubuntu-latest
20+
name: Terratest Checks
21+
22+
env:
23+
PR_NUMBER: >-
24+
${{ github.event_name == 'workflow_dispatch' &&
25+
github.event.inputs.pr_number || github.event.pull_request.number }}
26+
27+
28+
steps:
29+
- name: Checkout PR code
30+
uses: actions/checkout@v4
31+
with:
32+
ref: refs/pull/${{ env.PR_NUMBER }}/head
33+
34+
- name: Configure AWS credentials via OIDC
35+
uses: aws-actions/configure-aws-credentials@v2
36+
with:
37+
role-to-assume: ${{ secrets.ARC_IAC_TERRATEST_ROLE }}
38+
aws-region: us-east-1
39+
40+
- name: Set up Go
41+
uses: actions/setup-go@v4
42+
with:
43+
go-version: '1.24'
44+
45+
- name: Set up Terraform
46+
uses: hashicorp/setup-terraform@v2
47+
with:
48+
terraform_version: 1.7.5
49+
terraform_wrapper: false
50+
51+
- name: Create test directory and download go from S3
52+
run: |
53+
mkdir -p terra-test
54+
aws s3 cp ${{ secrets.ARC_TERRATEST_GO_FILE }} terra-test/terra_test.go
55+
- name: Initialize Go module and install dependencies
56+
run: |
57+
cd terra-test
58+
ls
59+
go mod init terraform-test || true
60+
go get github.com/gruntwork-io/terratest/modules/terraform
61+
go get github.com/stretchr/testify/assert
62+
go mod tidy
63+
go test -v -timeout 50m
64+
- name: Report check status manually
65+
uses: actions/github-script@v7
66+
with:
67+
github-token: ${{ secrets.GITHUB_TOKEN }}
68+
script: |
69+
const pr_number = parseInt(process.env.PR_NUMBER);
70+
const pr = await github.rest.pulls.get({
71+
owner: context.repo.owner,
72+
repo: context.repo.repo,
73+
pull_number: pr_number,
74+
});
75+
const sha = pr.data.head.sha;
76+
await github.rest.repos.createCommitStatus({
77+
owner: context.repo.owner,
78+
repo: context.repo.repo,
79+
sha: sha,
80+
state: 'success',
81+
context: 'terratest',
82+
description: 'Manual terratest completed successfully',
83+
target_url:
84+
`https://github.com/${context.repo.owner}/${context.repo.repo}` +
85+
`/actions/runs/${process.env.GITHUB_RUN_ID}`,
86+
});
File renamed without changes.

examples/data.tf renamed to example/data.tf

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,20 @@ data "aws_vpc" "vpc" {
66
}
77
}
88

9-
## Network
9+
## Network - Public Subnets
1010
data "aws_subnets" "this" {
1111
filter {
1212
name = "tag:Name"
1313
values = [
14-
"${var.project_name}-${var.environment}-subnet-${var.region}a",
15-
"${var.project_name}-${var.environment}-subnet-${var.region}b"
14+
"${var.project_name}-${var.environment}-public-subnet-public-${var.region}a",
15+
"${var.project_name}-${var.environment}-public-subnet-public-${var.region}b"
1616
]
1717
}
18+
19+
filter {
20+
name = "vpc-id"
21+
values = [data.aws_vpc.vpc.id]
22+
}
1823
}
1924

2025
## Security
@@ -32,9 +37,9 @@ data "aws_security_groups" "this" {
3237

3338
## Secrets Manager
3439
data "aws_secretsmanager_secret" "source-secret" {
35-
name = "source-secret"
40+
name = "arc-poc-rds-connection-details"
3641
}
3742

3843
data "aws_secretsmanager_secret" "target-secret" {
39-
name = "target-secret"
44+
name = "arc-dev-target-database-connection"
4045
}

examples/main.tf renamed to example/main.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ module "aws_dms" {
4343
instance_allocated_storage = 5
4444
instance_apply_immediately = true
4545
instance_network_type = "IPV4"
46-
instance_class = "dms.t2.micro"
47-
instance_id = "DMS-POC"
46+
instance_class = "dms.t3.micro"
47+
instance_id = "dms-poc"
4848
instance_subnet_group_id = "dms-poc-public-subnet-group"
4949
instance_publicly_accessible = true
5050
instance_vpc_security_group_ids = data.aws_security_groups.this.ids #Security Group ID
@@ -54,7 +54,7 @@ module "aws_dms" {
5454
endpoint_id = "dms-poc-endpoint-1"
5555
endpoint_type = "source"
5656
engine_name = "postgres"
57-
database_name = "poc"
57+
database_name = "poc_source"
5858
secrets_manager_arn = data.aws_secretsmanager_secret.source-secret.arn #Source endpoint secret arn
5959
ssl_mode = "require"
6060

example/output.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "replication_instance_arn" {
2+
description = "The Amazon Resource Name (ARN) of the replication instance"
3+
value = module.aws_dms.replication_instance_arn
4+
}

examples/variables.tf renamed to example/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ variable "environment" {
2424
variable "project_name" {
2525
description = "Name of the project the vpn resource belongs to."
2626
type = string
27-
default = "arc-example"
27+
default = "arc"
2828
}

modules/dms/main.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ resource "aws_iam_role_policy" "dms_secrets_manager_access_policy" {
4040
Effect = "Allow",
4141
Action = "iam:PassRole",
4242
Resource = aws_iam_role.dms_secrets_manager_access_role.arn
43+
},
44+
{
45+
Effect = "Allow",
46+
Action = [
47+
"kms:Decrypt",
48+
"kms:DescribeKey"
49+
],
50+
Resource = "*"
4351
}
4452
]
4553
})

0 commit comments

Comments
 (0)