DEV Community

John Walker
John Walker

Posted on

Deploying Terraform Code via AWS CodeBuild and AWS CodePipeline

In a previous post i demonstrated how to use Terraform to deploy an Amazon API Gateway backed by Lambda. This was an example of how you can use Terraform as Infrastructure as Code to manage your resources in AWS.

https://dev.to/aws-builders/deploying-amazon-api-gateway-and-lambda-with-terraform-1i2o

However, the way of deploying the Terraform code to AWS was to manually run the terraform plan and apply steps locally each time we want to update the API Gateway or our Lambda code. We can improve on this and automate the deployment of the Terraform Code using AWS CodePipeline and CodeBuild.

To do this, we will still need to the Deploy the CICD Pipeline initially locally (or from a machine in AWS), which will need to be monitored to update the pipeline itself, but this Pipeline will allow any code committed to the GitHub Repository to be deployed into AWS Automatically, and should only need minimal maintenance as it should not change on the same frequency as the API Gateway / Lambda Code.

To ensure we don't just blindly deploy code that hasn't been checked, we'll split this out into stages:

  • CodePipeline

    • Download Source Code from API Gateway Repository
    • Run a Planning Step in AWS CodeBuild.
      • Download and Install Terraform
      • Initialise the Terraform Environment with an S3 Backend
      • Run the Terraform Plan, and save the output to an Artifact
    • Send an Email via SNS to say the pipeline is awaiting approval
      • Await Manual Approval
    • Run an Apply Step in AWS CodeBuild.
      • Download and Install Terraform
      • Initialise the Terraform Environment with an S3 Backend
      • Run the Terraform Apply using the Artifact from the Planning stage

Top comments (0)