This section shows how you can use Github actions to automate the deployment of a cloudformation stack in terraform
Github Actions (aws.yml)
- Sets up terraform and aws configurations
- Formats, initialises and validates terraform
- Executes terraform plan
- Applies terraform plan
Terraform (aws.tf)
- uses the aws_cloudformation_stack resource to create a stack "Test-Stack"
- parameter (bucketID) is gotten from the
terraform
commands in aws.yml and parsed as a variableID
- template body is retrieved from the file
frontend.yml
Cloudformation (frontend.yml)
- Creates a bucket with the bucket name =
github-run-$bucketID
($bucketID is the parameter from aws.tf) - Exports the bucket URL as BucketID
Terraform destroys the deployment on successful completion.
To run this deployment, please do the following:
- Fork this git
Well, I always say this, don't I? - Create the following environment variables
- TF_API_TOKEN
- To create this, you need to do the following:
- Create an account at https://app.terraform.io
- Create an organisation and workspace
- install terraform, run
terraform login
. You will be guided on how to create a token
The token is usually saved in terraform.d/credentials.tfrc.json
- To create this, you need to do the following:
- Create Repository secrets for AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_DEFAULT_REGION
- To create a secret, follow this guide
- If you don't have these three secrets, follow the guide to create an IAM user with programmatic access
- Push a change to your repository