Skip to content

Commit c3056e7

Browse files
authored
Merge pull request #3 from pagopa/update-actions-and-readme
Actions update and docs
2 parents cc28791 + 6c9de45 commit c3056e7

File tree

7 files changed

+115
-58
lines changed

7 files changed

+115
-58
lines changed

.github/workflows/apply/action.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: apply
2+
description: "Run terraform apply"
3+
4+
inputs:
5+
env:
6+
required: true
7+
type: string
8+
working-directory:
9+
required: true
10+
type: string
11+
12+
runs:
13+
using: "composite"
14+
steps:
15+
- name: Read terraform version
16+
id: read-version
17+
shell: bash
18+
working-directory: ${{ inputs.working-directory }}
19+
run: |
20+
echo "TERRAFORM_VERSION=`cat ../.terraform-version`" >> $GITHUB_ENV
21+
22+
- name: Setup Terraform
23+
uses: hashicorp/setup-terraform@633666f66e0061ca3b725c73b2ec20cd13a8fdd1
24+
with:
25+
terraform_version: ${{ env.TERRAFORM_VERSION }}
26+
27+
- name: Terraform Init
28+
id: init
29+
shell: bash
30+
working-directory: ${{ inputs.working-directory }}
31+
run: |
32+
e=${{ inputs.env }}
33+
./terraform.sh init ${e%"_w"}
34+
35+
- name: Terraform Apply
36+
id: apply
37+
shell: bash
38+
working-directory: ${{ inputs.working-directory }}
39+
run: |
40+
e=${{ inputs.env }}
41+
./terraform.sh apply ${e%"_w"} -auto-approve

.github/workflows/terraform-apply.yml

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -21,69 +21,70 @@ on:
2121
environment:
2222
description: 'Which environment to update.'
2323
type: choice
24-
default: uat
25-
required: true
24+
required: true
25+
default: dev
2626
options:
27+
- dev
2728
- uat
28-
- prod
2929

30-
defaults:
31-
run:
32-
shell: bash
33-
working-directory: src/main
3430

3531
jobs:
36-
get-env:
37-
name: "get-environment"
38-
runs-on: ubuntu-latest
39-
steps:
40-
- name: Checkout
41-
uses: actions/checkout@v2
42-
- name: Get Environment
43-
id: get-env
44-
run: |
45-
USER_INPUT=${{ github.event.inputs.environment }}
46-
echo "ret=${USER_INPUT:-"uat"}" >> $GITHUB_OUTPUT
47-
outputs:
48-
ret: ${{ steps.get-env.outputs.ret }}
49-
terraform:
50-
needs: [get-env]
32+
manual-trigger:
33+
if: "${{ github.event.inputs.environment != '' }}"
5134
name: "terraform-apply"
5235
runs-on: ubuntu-latest
36+
continue-on-error: false
5337
permissions:
5438
id-token: write # This is required for requesting the JWT
5539
contents: read # This is required for actions/checkout
56-
environment:
57-
name: ${{ needs.get-env.outputs.ret }}
40+
environment: ${{ inputs.environment }}
41+
5842
steps:
5943
- name: Checkout
60-
uses: actions/checkout@v2
61-
44+
uses: actions/checkout@v3
45+
6246
- name: Configure AWS Credentials
6347
env:
6448
AWS_REGION: eu-south-1
65-
66-
uses: aws-actions/configure-aws-credentials@v1
49+
uses: aws-actions/configure-aws-credentials@67fbcbb121271f7775d2e7715933280b06314838
6750
with:
6851
role-to-assume: ${{ secrets.IAM_ROLE }}
6952
aws-region: ${{ env.AWS_REGION }}
70-
71-
- name: Read terraform version
72-
id: read-version
73-
run: |
74-
echo "TERRAFORM_VERSION=`cat ../.terraform-version`" >> $GITHUB_ENV
75-
76-
- name: Setup Terraform
77-
uses: hashicorp/setup-terraform@v2
53+
54+
- name: Apply
55+
uses: ./.github/workflows/apply
7856
with:
79-
terraform_version: ${{ env.TERRAFORM_VERSION }}
57+
env: ${{ inputs.environment }}
58+
working-directory: src/main
59+
60+
merge-trigger:
61+
if: "${{ github.event.inputs.environment == '' }} && github.ref == 'refs/heads/main'"
62+
name: "terraform-apply"
63+
strategy:
64+
max-parallel: 1
65+
matrix:
66+
environment: [dev, uat, prod_w]
67+
runs-on: ubuntu-latest
68+
continue-on-error: false
69+
environment: ${{ matrix.environment }}
70+
permissions:
71+
id-token: write # This is required for requesting the JWT
72+
contents: read # This is required for actions/checkout
8073

81-
- name: Terraform Init
82-
id: init
83-
run: |
84-
./terraform.sh init ${{ needs.get-env.outputs.ret }}
74+
steps:
75+
- name: Checkout
76+
uses: actions/checkout@v3
8577

86-
- name: Terraform Apply
87-
id: apply
88-
run: |
89-
./terraform.sh apply ${{ needs.get-env.outputs.ret }} -auto-approve
78+
- name: Configure AWS Credentials
79+
env:
80+
AWS_REGION: eu-south-1
81+
uses: aws-actions/configure-aws-credentials@67fbcbb121271f7775d2e7715933280b06314838
82+
with:
83+
role-to-assume: ${{ secrets.IAM_ROLE }}
84+
aws-region: ${{ env.AWS_REGION }}
85+
86+
- name: Apply
87+
uses: ./.github/workflows/apply
88+
with:
89+
env: ${{ matrix.environment }}
90+
working-directory: src/main

.github/workflows/terraform-plan.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
name: "Terraform-Plan"
22

33
on:
4-
pull_request:
4+
push:
5+
branches:
6+
- '**' # matches every branch
7+
- '!main' # excludes main
8+
- '!master' # excludes master
59
paths:
610
- 'src/main/**'
711
- '.github/workflows/*.yml'
@@ -19,7 +23,7 @@ jobs:
1923
terraform:
2024
strategy:
2125
matrix:
22-
environment: [uat, prod]
26+
environment: [dev, uat, prod]
2327
name: "terraform-plan"
2428
runs-on: ubuntu-latest
2529
environment: ${{ matrix.environment }}
@@ -31,7 +35,7 @@ jobs:
3135
- name: Configure AWS Credentials
3236
env:
3337
AWS_REGION: eu-south-1
34-
uses: aws-actions/configure-aws-credentials@v1
38+
uses: aws-actions/configure-aws-credentials@67fbcbb121271f7775d2e7715933280b06314838
3539
with:
3640
role-to-assume: ${{ secrets.IAM_ROLE }}
3741
aws-region: ${{ env.AWS_REGION }}
@@ -42,7 +46,7 @@ jobs:
4246
echo "TERRAFORM_VERSION=`cat ../.terraform-version`" >> $GITHUB_ENV
4347
4448
- name: Setup Terraform
45-
uses: hashicorp/setup-terraform@v2
49+
uses: hashicorp/setup-terraform@633666f66e0061ca3b725c73b2ec20cd13a8fdd1
4650
with:
4751
terraform_version: ${{ env.TERRAFORM_VERSION }}
4852

README.md

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# Project Name
2-
Template useful to create a AWS terraform projects
2+
Template useful to create a AWS infrastructures with terraform
33

44

55
## Howo to use this template
66

77
1. Create your github repository starting form this template.
8-
2. Configure your aws cli and set the [credentials](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html). Also refer the conflunece page to work with [AWS SSO](https://pagopa.atlassian.net/wiki/spaces/DEVOPS/pages/466846955/AWS+-+Users+groups+and+roles#SSO-with-GSuite).
9-
3. The __src/init__ directory contains the terraform code to setup the S3 backend the Dynamodb lock table and the iam role to use in the github actions
10-
4. The __src/main__ directory cointains the terraform code to setup the infrastructure core infrastructure.
8+
2. Configure your **aws cli** and set the [credentials](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html). Also refer the confluence page to work with [AWS SSO](https://pagopa.atlassian.net/wiki/spaces/DEVOPS/pages/466846955/AWS+-+Users+groups+and+roles#SSO-with-GSuite).
9+
3. The __./src/init__ directory contains the terraform code to setup the S3 backend, the Dynamodb lock table, github openid connection and the iam role to use in the github actions
10+
4. The __./src/main__ directory cointains the terraform code to setup the core infrastructure.
1111
5. The __.github/workflows__ directory contains two yaml files to run a terraform plan and apply actions. They need a github environment secret to be created: IAM_ROLE (see below.)
1212

1313
## Requirements
1414

15-
You need the following tools to work on your laptop
15+
The following tools are required to setup the project locally.
1616

1717
1. [aws cli](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) installed.
1818
2. [tfenv](https://github.com/tfutils/tfenv) to mange terraform versions.
@@ -47,15 +47,26 @@ cd src/main
4747

4848
In the repository two github actions are already provided:
4949

50-
* **terraform-plan**: it runs every time a new Pull request(PR) is created and every time a push is made within the PR branch. It runs terraform plan through all the environments.
50+
* **terraform-plan**: it runs every time new code is pushed in every branch excluded main and master. It runs terraform plan through all the environments in parallel.
51+
52+
* **terraform-apply**: it run terraform apply in all the environments once a PR is merged with main.
53+
* The apply in PROD should require an approval: it depends on the Environment protection rules.
54+
* It can also be triggerd manually in all the environment expect main.
55+
56+
57+
![](./docs/gitaction-workflow.png)
5158

52-
* **terraform-apply**: it runs terraform apply in one of the environment (uat by default) every time a PR is merged into main branch or it can be triggered manually and users have the facolty to choose on which environment to apply it.
5359

5460
### Configurations
5561

56-
* Create [github environment](https://docs.github.com/en/actions/deployment/targeting-different-environments/using-environments-for-deployment) for uat and prod.
62+
* Create [github environment](https://docs.github.com/en/actions/deployment/targeting-different-environments/using-environments-for-deployment) for every environments:**dev**, **uat**, **prod**, and **prod_w**.
63+
* **prod_w** is like prod but it is meant to be used only in the apply action in production.
64+
65+
![](docs/github-environments.png)
5766

58-
* In each environmen create a secret named **IAM_ROLE** and set as a value the **arn** of the role created at the very beginnig when you set up the environment.
67+
* In each environment create a secret named **IAM_ROLE** and set its value with the **arn** of the role created at the very beginnig (init).
68+
* Within **prod_w** set two **Environment protection rules** as shown in the screenshot below:
69+
![](docs/protection-rules-and-secrets.png)
5970

6071

6172
## Referencees

docs/gitaction-workflow.png

122 KB
Loading

docs/github-environments.png

57.8 KB
Loading
98.9 KB
Loading

0 commit comments

Comments
 (0)