-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Enrico Goerlitz
committed
Jun 15, 2024
1 parent
d15a415
commit 4c35bf3
Showing
9 changed files
with
232 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
terraform/config/iam-tf-infrastructure-deployment-policy.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
terraform { | ||
backend "s3" {} | ||
} |