Host Frontend API Gateway-S3 Integration with Terraform
Translating CloudFormation template to Terraform configuration files
Table of contents
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
The Terraform module can be found here which uses the
body
argument to import OpenApi specificationAn 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.
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.
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
betweenaws_api_gateway_method
andaws_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 sourceaws_caller_identity
. I followed the example in its documentation to retrieve the AWS Account Id .