-
Notifications
You must be signed in to change notification settings - Fork 0
Home
This repository contains Terraform modules that deploy resources in AWS to demonstrate how you can use Terratest to write automated tests for your AWS Terraform code. It fulfills the following requirements:
-
Create Terraform code to create an AWS S3 bucket with two files: test1.txt and test2.txt. The content of these files must be the timestamp when the code was executed.
-
Using Terratest, create the test automation for the Terraform code, validating that both files and the bucket are created successfully.
-
Setup GitHub Actions to run a pipeline to validate this code.
-
Publish your code in a public GitHub repository, and share a Pull Request with your code. Do not merge into master until the PR is approved.
-
Include documentation describing the steps to run and test the automation.
The following steps are needed to complete the tasks listed above:
-
Create a free-tier account with AWS.
-
As a security best practice, create an IAM user with administrative privilege for S3 bucket.
-
Download your AWS account's keypair.
-
Install AWS Command Line Interface (CLI) on your computer.
-
Globally configure your AWS credentials by running
aws configure(on your command line) and entering your AWS credentials, OR set your environment variables with your credentials.
-
Configure your AWS credentials using one of the supported methods for AWS CLI tools, such as setting the
AWS_ACCESS_KEY_IDandAWS_SECRET_ACCESS_KEYenvironment variables. If you are using the~/.aws/configfile for profiles thenexport AWS_SDK_LOAD_CONFIGas"True". -
You can set the AWS region you want to use as the environment variable
AWS_DEFAULT_REGION. -
Install Terraform and make sure it is on your PATH.
-
Run
terraform init. -
Run
terraform apply. -
When you are done creating your resources, run
terraform destroy.
Terratest runs all your terraform code in the same steps as those done by terraform. These include running terraform init, terraform apply, reading the output variable using terraform output, checking to ensure that its value is what we expect, and running terraform destroy (using defer to run it at the end of the test, whether the test succeeds or fails). However, to get to this point, do the following:
-
Install Golang and make sure your code is checked out into your
GOPATH. -
cdinto the folder containing your terratest code. -
Run
go mod init <your github.com/<YOUR_USERNAME>/<YOUR_REPO_NAME>. Note: This step assumes you have created a Github account and a repository dedicated to your code. -
Run
go get <packages required to run your test>. -
Run
go mod tidy. -
Run
go test -vin the folder containing your Terratest file.
Based on your GitHub Workflow file content, GitHub Actions may require your AWS credentials, including region, access key ID, and secret access key. The values you provide for these keys will be the same as your AWS credentials.
Your GitHub Workflow file may contain actions for the following - validate, init, format, plan, apply, and so on. It depends on the events and actions that you state in the file.