Skip to content

Commit

Permalink
cd-terraform added
Browse files Browse the repository at this point in the history
  • Loading branch information
Enrico Goerlitz committed Jun 15, 2024
1 parent d15a415 commit 4c35bf3
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 7 deletions.
110 changes: 110 additions & 0 deletions .github/workflows/cd-terraform.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
name: Terraform Deployment

on:
push:
branches:
- main
pull_request:
branches:
- main

permissions:
id-token: write # This is required for aws oidc connection
contents: read # This is required for actions/checkout
pull-requests: write # This is required for gh bot to comment PR

env:
TF_LOG: INFO
AWS_REGION: ${{ secrets.AWS_REGION }}

jobs:
deploy:
runs-on: ubuntu-latest

defaults:
run:
shell: bash
working-directory: ./terraform

steps:
- name: Git checkout
uses: actions/checkout@v3

- name: Configure AWS credentials from AWS account
uses: aws-actions/configure-aws-credentials@v1
with:
role-to-assume: ${{ secrets.AWS_ROLE }}
aws-region: ${{ secrets.AWS_REGION }}
role-session-name: GitHub-OIDC-TERRAFORM

- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_version: 1.5.7

- name: Terraform fmt
id: fmt
run: terraform fmt -check
continue-on-error: true

- name: Terraform Init
id: init
env:
AWS_BUCKET_NAME: ${{ secrets.AWS_BUCKET_NAME }}
AWS_BUCKET_KEY_NAME: ${{ secrets.AWS_BUCKET_KEY_NAME }}
run: terraform init -backend-config="bucket=${AWS_BUCKET_NAME}" -backend-config="key=${AWS_BUCKET_KEY_NAME}" -backend-config="region=${AWS_REGION}"

- name: Terraform Validate
id: validate
run: terraform validate -no-color

- name: Terraform Plan
id: plan
run: terraform plan -no-color
if: github.event_name == 'pull_request'
continue-on-error: true

- uses: actions/github-script@v6
if: github.event_name == 'pull_request'
env:
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\`
#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
#### Terraform Validation 🤖\`${{ steps.validate.outcome }}\`
<details><summary>Validation Output</summary>
\`\`\`\n
${{ steps.validate.outputs.stdout }}
\`\`\`
</details>
#### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
<details><summary>Show Plan</summary>
\`\`\`\n
${process.env.PLAN}
\`\`\`
</details>
*Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
- name: Terraform Plan Status
if: steps.plan.outcome == 'failure'
run: exit 1

- name: Terraform Apply
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
run: terraform apply -auto-approve -input=false
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,42 @@ $ docker push enricogoerlitz/bp2-backend-amd64v2:latest

https://www.youtube.com/watch?v=GowFk_5Rx_I&ab_channel=CloudScalr

deploy terraform on S3 and manage this in S3
deploy terraform on S3 and manage this in S3

## Doku OpenIDConnect

aws > IAM > Identity Provider > new Identity Provider
- url=https://token.actions.githubusercontent.com
- audience=sts.amazonaws.com

enricogoerlitz/aws-bp-2-hosting-backend-on-ec2-asg-alb

aws > s3 > create bucket
- name
- enable enrcyption

aws > IAM > roles > create role > custom trusted policy
policy:
{
"Version": "2008-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::YOUR_ACCOUNT_NUMBER:oidc-provider/token.actions.githubusercontent.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:YOUR_GITHUB_USERNAME/YOUR_REPO_NAME:*"
}
}
}
]
}

GitHub Secrets:
- AWS_BUCKET_NAME=bp2-terraform-deployment-state
- AWS_BUCKET_KEY_NAME=infra.tfstate
- AWS_REGION=eu-central-1
- AWS_ROLE=arn:aws:iam::533267024986:role/github-oicd-bp2-terraform-deployment-role
12 changes: 6 additions & 6 deletions app/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,29 @@ def test_healthcheck(self):
hostname = os.uname()[1]

# WHEN
response = self.app.get('/')
response = self.app.get("/")
data = response.get_json()

# THEN
self.assertEqual(response.status_code, 200)
self.assertEqual(data['healthcheck'], 'ok')
self.assertEqual(data['hostname'], hostname)
self.assertEqual(data["healthcheck"], "ok")
self.assertEqual(data["hostname"], hostname)

def test_host_ip(self):
# GIVEN
ip = os.uname()[1]

# WHEN
response = self.app.get('/host')
response = self.app.get("/host")
data = response.get_json()

# THEN
self.assertEqual(response.status_code, 200)
self.assertEqual(data['hostname'], ip)
self.assertEqual(data["hostname"], ip)

# def test_fail(self):
# self.assertEqual(True, False)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
File renamed without changes.
17 changes: 17 additions & 0 deletions terraform/config/iam-role-trusted-entity.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::533267024986:oidc-provider/token.actions.githubusercontent.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:enricogoerlitz/aws-bp-2-hosting-backend-on-ec2-asg-alb:ref:refs/heads/main"
}
}
}
]
}
17 changes: 17 additions & 0 deletions terraform/config/iam-s3-access-policy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::bp2-terraform-deployment-state/*",
"arn:aws:s3:::bp2-terraform-deployment-state"
]
}
]
}
40 changes: 40 additions & 0 deletions terraform/config/iam-tf-infrastructure-deployment-policy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ec2:Describe*",
"ec2:RunInstances",
"ec2:CreateSecurityGroup",
"ec2:AuthorizeSecurityGroupIngress",
"ec2:CreateKeyPair",
"ec2:CreateLaunchTemplate",
"ec2:ModifyLaunchTemplate",
"ec2:TerminateInstances",
"autoscaling:CreateAutoScalingGroup",
"autoscaling:UpdateAutoScalingGroup",
"autoscaling:Describe*",
"elasticloadbalancing:CreateLoadBalancer",
"elasticloadbalancing:CreateTargetGroup",
"elasticloadbalancing:CreateListener",
"elasticloadbalancing:ModifyListener",
"elasticloadbalancing:DeleteListener",
"elasticloadbalancing:Describe*",
"route53:ChangeResourceRecordSets",
"route53:GetHostedZone",
"acm:DescribeCertificate",
"acm:GetCertificate",
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": "acm:ListCertificates",
"Resource": "*"
}
]
}
File renamed without changes.
3 changes: 3 additions & 0 deletions terraform/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform {
backend "s3" {}
}

0 comments on commit 4c35bf3

Please sign in to comment.