Deploying custom domain rest api

From mouadja02/skills · View interactive version

Deploying custom domain rest api is an AI agent skill from the GitHub repository mouadja02/skills. Deploys a Regional REST API with a custom domain name, a Lambda backend function, and a request-based Lambda authorizer using AWS CLI. Covers ACM certificate provisioning, API Gateway REST API creation, Lambda function deployment, request authorizer setup, custom domain configuration, base path mapp

How to use this skill

Skill file

Attribution: Sourced from aws/agent-toolkit-for-aws by Amazon Web Services, licensed Apache-2.0. Modified for this collection.

Custom Domain REST API with Lambda and Request Authorizer

Overview

This SOP deploys a REST API with a Regional custom domain name, a Lambda backend function, and a request-based Lambda authorizer. It handles ACM certificate provisioning, IAM role creation, Lambda function deployment, API Gateway REST API creation with a custom authorizer, custom domain configuration, base path mapping, and Route 53 DNS setup.

The architecture includes:

Important: This SOP uses Regional endpoints. If the user requests a private endpoint, inform them that this skill covers Regional endpoints only. Private endpoints require VPC endpoint configuration.

Parameters

Constraints for parameter acquisition:

Steps

0. Verify Dependencies

Constraints:

1. Retrieve AWS Account ID

This step MUST be performed before all other steps.

Constraints:

2. Request ACM Certificate

Skip this step if acm_certificate_arn is already provided.

Constraints:

3. Create IAM Execution Roles

Constraints:

4. Create and Deploy Lambda Functions

Constraints:

python3 -c "import zipfile,io,base64; z=io.BytesIO(); f=zipfile.ZipFile(z,'w'); f.writestr('index.mjs', open('scripts/authorizer.mjs').read()); f.close(); open('/tmp/authorizer.zip','wb').write(z.getvalue())"

python3 -c "import zipfile,io; z=io.BytesIO(); f=zipfile.ZipFile(z,'w'); f.writestr('index.mjs', open('scripts/example_function.mjs').read()); f.close(); open('/tmp/example_function.zip','wb').write(z.getvalue())"

5. Create REST API with Request Authorizer

Constraints:

6. Deploy the API

Constraints:

7. Create Custom Domain and Base Path Mapping

Constraints:

8. Create Route 53 DNS Record

Constraints:

9. Validate Final Setup

Constraints:

Examples

Example Input

custom_domain_name: api.example.com
region: us-east-2
hosted_zone_id: Z2OJLYMUO9EFXC
stage_name: prod

Example Output

ACM certificate issued for api.example.com
  ARN: arn:aws:acm:us-east-2:123456789012:certificate/abc-123

IAM roles created
  Authorizer: arn:aws:iam::123456789012:role/request-authorizer-role
  Example: arn:aws:iam::123456789012:role/example-function-role

Lambda functions deployed
  Authorizer: arn:aws:lambda:us-east-2:123456789012:function:request-authorizer
  Example: arn:aws:lambda:us-east-2:123456789012:function:example-function

REST API deployed
  API ID: a1b2c3d4e5
  Stage: prod (StageVar1=stageValue1)
  Authorizer: request-authorizer (REQUEST type)

Custom domain configured
  Domain: api.example.com
  Regional endpoint: d-abc123.execute-api.us-east-2.amazonaws.com
  TLS: 1.2

Route 53 DNS record created
  A-alias: api.example.com -> d-abc123.execute-api.us-east-2.amazonaws.com

Test command (authorized):
  curl 'https://api.example.com/example?QueryString1=queryValue1' -H 'HeaderAuth1: headerValue1'

Test command (denied):
  curl 'https://api.example.com/example'

Troubleshooting

Certificate Stuck in PENDING_VALIDATION

Verify the DNS validation CNAME record exists in Route 53 by running aws acm describe-certificate --certificate-arn {arn} --query 'Certificate.DomainValidationOptions'. Ensure the CNAME was created in the correct hosted zone.

403 Forbidden on API Calls

The request authorizer checks three values: HeaderAuth1 header must be headerValue1, QueryString1 query parameter must be queryValue1, and stage variable StageVar1 must be stageValue1. Verify all three are present and correct. Check CloudWatch Logs for the authorizer function for detailed error messages.

401 Unauthorized

API Gateway returns 401 when the authorizer function cannot be invoked. Verify the Lambda permission was added for API Gateway to invoke the authorizer. Check that the authorizer URI is correct.

Missing Authentication Token (403)

The request path doesn't match a configured resource. Verify the /example resource exists with aws apigateway get-resources --rest-api-id {api_id}. Ensure the API was deployed after creating all resources.

Custom Domain Returns No Response

DNS propagation can take up to 48 hours. Check with dig {custom_domain_name}. Verify the A-alias record points to the correct regionalDomainName and regionalHostedZoneId from the create-domain-name response.

Stage Variable Not Set

If the authorizer denies all requests, verify the stage variable was set with aws apigateway get-stage --rest-api-id {api_id} --stage-name {stage_name} --query 'variables'. The StageVar1 variable must be set to stageValue1.

IAM Role Not Found When Creating Lambda

IAM role propagation is eventually consistent. Wait at least 10 seconds after role creation before creating Lambda functions. Verify the role ARN with aws iam get-role --role-name {role_name}.

Base Path Mapping Not Working

Verify with aws apigateway get-base-path-mappings --domain-name {custom_domain_name}. The base path (none) maps the domain root to the stage. Ensure the deployment to the stage completed successfully.

Security Considerations

Additional Resources

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.