Skip to main content

Command Palette

Search for a command to run...

Host Frontend API Gateway-S3 Integration with Terraform

Translating CloudFormation template to Terraform configuration files

Updated
2 min read
Host Frontend API Gateway-S3 Integration with Terraform
B

A fledgling engineer dabbling into areas of DevOps, AWS and automation. I enjoy tinkering with technology frameworks and tools to understand and gain visibility in the underlying mechanisms of the "magic" in them.

In the progress of accumulating nuggets of wisdom in the different software engineering disciplines!

Overview

To know more about the configurations in API Gateway, read the original article when provisioning using CloudFormation

  • To practice writing Terraform configurations, I will be referencing the API Gateway - S3 integration architecture that I have defined in this CloudFormation Template.

  • This will be a good practice as it involves resources interconnecting with each other, rather than just provisioning standalone resources.

TLDR; Resources

  1. The Terraform module can be found here which uses the body argument to import OpenApi specification

  2. An alternative way of provisioning is via Terraform resources.

# Clone the repo
git clone https://github.com/bernicecpz/hashnode_resources.git

# Navigate to the terraform directory
cd terraform_modules/aws

# Initialize the workspace directory containing the Terraform config files
terraform init

# Create an execution plan to preview the changes that Terraform plans to make to your infrastructure
terraform plan

# Apply the changes to your AWS infrastructure
terraform apply

Biggest Challenge

Upon completing this exercise, I believe my initial difficulties was more of unfamiliarity to the Terraform's configuration language and my preference of learning through examples.

  1. The biggest difference between CloudFormation and Terraform is how one can define the API paths.

    • Initially I face difficulties which key-value pair to define as part of the body as well as when should one enclose the key with double quotes.

    • I overcame this by referencing exported Open Api JSON file.

  2. I use the API Gateway Terraform resources i.e. resources (aws_api_gateway_resource), methods (aws_api_gateway_method) and integrations (aws_api_gateway_integration). This eventually allow me to grasp the OpenAPI format.

    • This method of provisioning requires one to be mindful that the `request_parameters` in the integrations MUST align with those defined in the methods.

    • One will also need to be explicit in defining the `resource_id` to ensure that the method (e.g. "`GET`") is defined under the right path.

References

  • In this repository, Hashicorp provide an example on how to provision S3-API Gateway Integration via Terraform resources.

  • This issue request contains the explanation with regards to the alignment of request_parameters between aws_api_gateway_method and aws_api_gateway_integration.

  • Pseudo parameters reference are values predefined by AWS CloudFormation. For the equivalent of ${AWS::AccountId} in Terraform, it involves the use of the data source aws_caller_identity. I followed the example in its documentation to retrieve the AWS Account Id .

Cloud

Part 2 of 11

I will be documenting directly and simply the how-tos on specific concepts while using cloud services. Join me in my learning journey!

Up next

Host Frontend with AWS API Gateway-S3 integration

Configure multiple frontends to be hosted on a single S3 bucket exposed through API Gateway using CloudFormation