Skip to content

Commit 9f6f8dd

Browse files
authored
Merge pull request #1 from pangolin-do-golang/feature/infra
Add infra
2 parents 89f8891 + 8aecf2d commit 9f6f8dd

18 files changed

+451
-1
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## Why
2+
<!-- Describe the context of the change here. Why are you creating or changing this resource? What application will be impacted? -->
3+
4+
## Pull request type
5+
- [ ] New resource
6+
- [ ] Update resource
7+
- [ ] Bugfix
8+
- [ ] Other (please describe):
9+
10+
## Rollback plan
11+
- [ ] Revert this PR
12+
- [ ] Other (please, specify):

.github/workflows/Develop.yml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
name: "Terraform Developer"
2+
3+
on:
4+
push:
5+
branches:
6+
- develop
7+
paths:
8+
- terraform/**
9+
pull_request:
10+
branches:
11+
- develop
12+
paths:
13+
- terraform/**
14+
15+
env:
16+
WORKSPACE: "dev"
17+
aws-region: "us-east-1"
18+
destroy: false
19+
20+
permissions:
21+
contents: read
22+
id-token: write
23+
pull-requests: write
24+
25+
jobs:
26+
InfraDev:
27+
runs-on: ubuntu-latest
28+
defaults:
29+
run:
30+
shell: bash
31+
working-directory: ./terraform
32+
33+
steps:
34+
- name: "Checkout"
35+
uses: actions/checkout@v4
36+
37+
- name: Setup Terraform
38+
uses: hashicorp/setup-terraform@v3
39+
with:
40+
terraform_version: 1.10.5
41+
42+
- name: Configure AWS credentials
43+
uses: aws-actions/configure-aws-credentials@v4
44+
with:
45+
role-session-name: GitHub_AWS_OIDC
46+
aws-region: ${{ env.aws-region }}
47+
role-to-assume: ${{ vars.AWS_ASSUME_ROLE_ARN }}
48+
49+
- name: Terraform fmt
50+
id: fmt
51+
run: terraform fmt -check
52+
continue-on-error: true
53+
54+
- name: Terraform Init
55+
id: init
56+
run: |
57+
terraform init -backend-config="./envs/${{ env.WORKSPACE }}/backend.tfvars" -input=false
58+
terraform workspace select -or-create ${{ env.WORKSPACE }}
59+
continue-on-error: false
60+
61+
- name: Terraform Validate
62+
id: validate
63+
run: terraform validate
64+
continue-on-error: true
65+
66+
- name: Terraform Destroy
67+
if: ${{ env.destroy == true }}
68+
id: destroy
69+
run: terraform destroy -var-file="./envs/${{ env.WORKSPACE }}/terraform.tfvars" -auto-approve
70+
71+
- name: Terraform Plan
72+
if: ${{ env.destroy != true && github.event_name == 'pull_request'}}
73+
id: plan
74+
run: terraform plan -out plan.tf -var-file="./envs/${{ env.WORKSPACE }}/terraform.tfvars" -no-color
75+
76+
- name: Terraform Resources List
77+
uses: borchero/terraform-plan-comment@v2
78+
if: github.event_name == 'pull_request'
79+
with:
80+
token: ${{ github.token }}
81+
planfile: plan.tf
82+
working-directory: ./terraform
83+
84+
- name: Terraform Apply
85+
if: ${{ env.destroy != true && github.event_name == 'push'}}
86+
id: apply
87+
run: terraform apply -var-file="./envs/${{ env.WORKSPACE }}/terraform.tfvars" -auto-approve

.github/workflows/Production.yml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
name: "Terraform Production"
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
paths:
8+
- terraform/**
9+
pull_request:
10+
branches:
11+
- main
12+
paths:
13+
- terraform/**
14+
15+
env:
16+
WORKSPACE: "prod"
17+
aws-region: "us-east-1"
18+
destroy: false
19+
20+
permissions:
21+
contents: read
22+
id-token: write
23+
pull-requests: write
24+
25+
jobs:
26+
InfraProd:
27+
runs-on: ubuntu-latest
28+
defaults:
29+
run:
30+
shell: bash
31+
working-directory: ./terraform
32+
33+
steps:
34+
- name: "Checkout"
35+
uses: actions/checkout@v4
36+
37+
- name: Setup Terraform
38+
uses: hashicorp/setup-terraform@v3
39+
with:
40+
terraform_version: 1.9.5
41+
42+
- name: Configure AWS credentials
43+
uses: aws-actions/configure-aws-credentials@v4
44+
with:
45+
role-session-name: GitHub_AWS_OIDC
46+
aws-region: ${{ env.aws-region }}
47+
role-to-assume: ${{ vars.AWS_ASSUME_ROLE_ARN }}
48+
49+
- name: Terraform Init
50+
id: init
51+
run: |
52+
terraform init -backend-config="./envs/${{ env.WORKSPACE }}/backend.tfvars" -input=false
53+
terraform workspace select -or-create ${{ env.WORKSPACE }}
54+
continue-on-error: false
55+
56+
- name: Terraform fmt
57+
id: fmt
58+
run: terraform fmt -check
59+
continue-on-error: true
60+
61+
- name: Terraform Validate
62+
id: validate
63+
run: terraform validate
64+
continue-on-error: true
65+
66+
- name: Terraform Destroy
67+
if: ${{ env.destroy == true }}
68+
id: destroy
69+
run: terraform plan -destroy -var-file="./envs/${{ env.WORKSPACE }}/terraform.tfvars"
70+
71+
- name: Terraform Plan
72+
if: ${{ env.destroy != true && github.event_name == 'pull_request'}}
73+
id: plan
74+
run: terraform plan -out plan.tf -var-file="./envs/${{ env.WORKSPACE }}/terraform.tfvars" -no-color
75+
76+
- name: Terraform Resouces List
77+
uses: borchero/terraform-plan-comment@v2
78+
if: github.event_name == 'pull_request'
79+
with:
80+
token: ${{ github.token }}
81+
planfile: plan.tf
82+
working-directory: ./terraform
83+
84+
- name: Terraform Apply
85+
if: ${{ env.destroy != true && github.event_name == 'push'}}
86+
id: apply
87+
run: terraform apply -var-file="./envs/${{ env.WORKSPACE }}/terraform.tfvars" -auto-approve

.github/workflows/tfdocs.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Validate and Generate Terraform Docs
2+
on:
3+
- pull_request
4+
5+
permissions:
6+
contents: write
7+
pull-requests: write
8+
9+
jobs:
10+
docs:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
with:
15+
ref: ${{ github.event.pull_request.head.ref }}
16+
17+
- name: Render terraform docs and push changes back to PR
18+
uses: terraform-docs/gh-actions@main
19+
with:
20+
working-dir: ./terraform
21+
output-file: README.md
22+
output-method: inject
23+
git-push: "true"

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ crash.*.log
1313
# password, private keys, and other secrets. These should not be part of version
1414
# control as they are data points which are potentially sensitive and subject
1515
# to change depending on the environment.
16-
*.tfvars
16+
# *.tfvars
1717
*.tfvars.json
18+
*.lock.hcl
19+
plan.out
1820

1921
# Ignore override files as they are usually used to override resources locally and so
2022
# are not checked in
@@ -35,3 +37,5 @@ override.tf.json
3537
# Ignore CLI configuration files
3638
.terraformrc
3739
terraform.rc
40+
.idea/
41+
.vscode/

.terraform-docs.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
content: |-
2+
{{ .Header }}
3+
4+
{{ .Inputs }}
5+
6+
{{ .Outputs }}

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,39 @@
11
# thumb-processor-infra
22
Infra do Projeto Hackaton
3+
4+
5+
### Pre Requirements To Run This Pipeline.
6+
7+
- Create a S3 Bucket in AWS Console (Unique Global Name)
8+
- Enable Versioning Bucket
9+
* Change the *Bucket Name and DynamoDB Table Name* in backend.conf files `(./terraform/envs/dev | ./terraform/envs/prod)`
10+
- Create a DynamoDB Table in AWS Console
11+
- Put In *Partition key* to *LockID* And Set *String* And *Crete Table*
12+
- Create a Identity Provider (OIDC) in IAM Console and add ARN on github variables with Name: *AWS_ASSUME_ROLE_ARN*
13+
- How To Create a OIDC Github
14+
- https://aws.amazon.com/pt/blogs/security/use-iam-roles-to-connect-github-actions-to-actions-in-aws/
15+
16+
17+
This Pipeline Runs With a Pull Request:
18+
- Branches:
19+
- developer - Runs Developer Infra
20+
- main - Runs Productions Infra
21+
22+
### Runs Localy
23+
24+
Get AWS Credentials And Runs:
25+
```shell
26+
aws configure # And Paste de SECRET_KEY_ID and SECRET_ACCESS_KEY and REGION
27+
28+
# After aws configure go to de Path /terraform and Runs: Config Backend S3
29+
terraform init -backend-config=envs/dev/backend.tfvars
30+
31+
# Set Dev Worspace
32+
terraform workspace new dev
33+
34+
# Runs Plan
35+
terraform plan -out plan.out -var-file=envs/dev/terraform.tfvars
36+
37+
# Aplly Plan
38+
terraform apply plan.out
39+
```

terraform/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!-- BEGIN_TF_DOCS -->
2+
3+
4+
## Inputs
5+
6+
| Name | Description | Type | Default | Required |
7+
|------|-------------|------|---------|:--------:|
8+
| <a name="input_bucket_name"></a> [bucket\_name](#input\_bucket\_name) | The name of the S3 bucket | `string` | n/a | yes |
9+
| <a name="input_env"></a> [env](#input\_env) | The environment | `string` | n/a | yes |
10+
| <a name="input_queue_name"></a> [queue\_name](#input\_queue\_name) | The name of the SQS queue | `string` | n/a | yes |
11+
12+
## Outputs
13+
14+
| Name | Description |
15+
|------|-------------|
16+
| <a name="output_queue_arn"></a> [queue\_arn](#output\_queue\_arn) | n/a |
17+
| <a name="output_queue_arn_static"></a> [queue\_arn\_static](#output\_queue\_arn\_static) | n/a |
18+
| <a name="output_queue_id"></a> [queue\_id](#output\_queue\_id) | n/a |
19+
| <a name="output_queue_name"></a> [queue\_name](#output\_queue\_name) | n/a |
20+
| <a name="output_queue_url"></a> [queue\_url](#output\_queue\_url) | n/a |
21+
| <a name="output_s3_bucket_arn"></a> [s3\_bucket\_arn](#output\_s3\_bucket\_arn) | The ARN of the bucket. Will be of format arn:aws:s3:::bucketname. |
22+
| <a name="output_s3_bucket_id"></a> [s3\_bucket\_id](#output\_s3\_bucket\_id) | The name of the bucket. |
23+
| <a name="output_s3_bucket_region"></a> [s3\_bucket\_region](#output\_s3\_bucket\_region) | The AWS region this bucket resides in. |
24+
| <a name="output_sqs_dlq_arn"></a> [sqs\_dlq\_arn](#output\_sqs\_dlq\_arn) | The ARN of the SQS queue |
25+
| <a name="output_sqs_dlq_id"></a> [sqs\_dlq\_id](#output\_sqs\_dlq\_id) | The URL for the created Amazon SQS queue |
26+
| <a name="output_sqs_dlq_name"></a> [sqs\_dlq\_name](#output\_sqs\_dlq\_name) | The name of the SQS queue |
27+
<!-- END_TF_DOCS -->

terraform/data.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data "aws_caller_identity" "current" {}

terraform/envs/dev/backend.tfvars

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
bucket = "hackaton-challanger-fiap"
2+
key = "terraform.tfstate"
3+
region = "us-east-1"
4+
dynamodb_table = "terraform-tfstate"

0 commit comments

Comments
 (0)