AWS API Gateway Terraform Module¶
terraform-aws-arc-api-gateway¶
Overview¶
The API Gateway Terraform module provides an easy way to create and manage REST APIs on AWS. It supports defining resources, methods (GET, POST, etc.), and integrations with Lambda functions or other AWS services. The module also supports adding usage plans and API keys, enabling you to manage access, throttle requests, and monitor API usage. This helps ensure secure, scalable, and well-governed API deployments.
Prerequisites¶
Before using this module, ensure you have the following:
- AWS credentials configured.
- Terraform installed.
- A working knowledge of Terraform.
Getting Started¶
- Define the Module
Initially, it's essential to define a Terraform module, which is organized as a distinct directory encompassing Terraform configuration files. Within this module directory, input variables and output values must be defined in the variables.tf and outputs.tf files, respectively. The following illustrates an example directory structure:
- Define Input Variables
Inside the variables.tf
or in *.tfvars
file, you should define values for the variables that the module requires.
- Use the Module in Your Main Configuration In your main Terraform configuration file (e.g., main.tf), you can use the module. Specify the source of the module, and version, For Example
- Output Values
Inside the outputs.tf
file of the module, you can define output values that can be referenced in the main configuration. For example:
- .tfvars
Inside the .tfvars
file of the module, you can provide desired values that can be referenced in the main configuration.
First Time Usage¶
uncomment the backend block in main.tf
terraform init
should be fine
Create a dev
workspace
Plan Terraform
Apply Terraform
Production Setup¶
Create a prod
workspace
Plan Terraform
Apply Terraform
Requirements¶
Name | Version |
---|---|
terraform | >= 1.3, < 2.0.0 |
aws | >= 5.0, < 6.0 |
Providers¶
Name | Version |
---|---|
aws | 5.100.0 |
terraform | n/a |
Modules¶
No modules.
Resources¶
Name | Type |
---|---|
aws_api_gateway_account.api_gateway_account | resource |
aws_api_gateway_api_key.this | resource |
aws_api_gateway_base_path_mapping.base_path_mapping | resource |
aws_api_gateway_deployment.this | resource |
aws_api_gateway_domain_name.custom_domain | resource |
aws_api_gateway_rest_api.this | resource |
aws_api_gateway_stage.this | resource |
aws_api_gateway_usage_plan.this | resource |
aws_api_gateway_usage_plan_key.this | resource |
aws_iam_role.api_gateway_account_role | resource |
aws_iam_role_policy.api_gateway_cloudwatch_policy | resource |
aws_route53_record.api_gateway_alias | resource |
terraform_data.replacement | resource |
aws_caller_identity.current | data source |
aws_region.current | data source |
aws_route53_zone.this | data source |
Inputs¶
Name | Description | Type | Default | Required |
---|---|---|---|---|
access_log_format | Acess log format for API Gateway | string |
null |
no |
api_gateway_domain | Configuration for API Gateway custom domain and Route53 record | object({ |
n/a | yes |
api_key_map | Configuration for API Gateway API keys. Each API key entry represents a client or application that requires access to the API, and can be associated with one or more usage plans. Fields: - name : The name of the API key (must be unique). - description : Optional description of the API key (e.g., which client it belongs to). - usage_plan : List of usage plan names (from var.usage_plan) this key should be associated with. This should match the names defined in var.usage_plan |
map(object({ |
{} |
no |
enable_cloudwatch_logs | Whether to enable CloudWatch logs for API Gateway | bool |
true |
no |
endpoint_type | API Gateway endpoint type. Valid values are 'EDGE', 'REGIONAL', or 'PRIVATE'. If not specified, defaults to 'REGIONAL'. | string |
"REGIONAL" |
no |
name | Name of the API Gateway REST endpoint | string |
n/a | yes |
open_api_json | Open API JSON file | string |
null |
no |
security_policy | The Transport Layer Security version for the custom domain. Valid values: TLS_1_0 or TLS_1_2 | string |
"TLS_1_2" |
no |
stage_name | API Gateway stage name | string |
n/a | yes |
tags | Tags for the resources | map(string) |
{} |
no |
usage_plan | Configuration for API Gateway usage plan | list(object({ |
[] |
no |
xray_tracing_enabled | Enable X-Ray tracing for Lambda | bool |
true |
no |
Outputs¶
Name | Description |
---|---|
api_keys | Map of API keys created (name => value) |
arn | The ARN of the API Gateway REST API |
custom_domain | The custom domain name for API Gateway (if created) |
custom_domain_regional_domain_name | The regional domain name for the custom domain (if created) |
custom_domain_regional_zone_id | The Route53 hosted zone ID for the custom domain |
deployment_id | The ID of the current API Gateway deployment |
execution_arn | The execution ARN of the API Gateway (useful for Lambda permissions) |
id | The ID of the API Gateway REST API |
invoke_url | Base invoke URL for the API Gateway stage |
stage_name | The name of the API Gateway stage |
usage_plans | Map of usage plans created |
Versioning¶
This project uses a .version
file at the root of the repo which the pipeline reads from and does a git tag.
When you intend to commit to main
, you will need to increment this version. Once the project is merged,
the pipeline will kick off and tag the latest git commit.
Development¶
Prerequisites¶
Configurations¶
- Configure pre-commit hooks
Versioning¶
while Contributing or doing git commit please specify the breaking change in your commit message whether its major,minor or patch
For Example
Tests¶
- Tests are available in
test
directory - Configure the dependencies
- Now execute the test
Authors¶
This project is authored by: - SourceFuse ARC Team