npx skills add ...
npx skills add aws/agent-toolkit-for-aws --skill deploying-custom-domain-rest-api
npx skills add aws/agent-toolkit-for-aws --skill deploying-custom-domain-rest-api
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 mapping, and Route 53 DNS record creation. Trigger keywords: custom domain, REST API, Lambda, Route 53, API Gateway, regional endpoint, request authorizer, base path mapping.
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:
GET /exampleImportant: 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.
api.example.com)Constraints for parameter acquisition:
Constraints:
This step MUST be performed before all other steps.
Constraints:
aws sts get-caller-identity --query 'Account' --output textSkip this step if acm_certificate_arn is already provided.
Constraints:
aws acm request-certificate --domain-name {custom_domain_name} --validation-method DNS --region {region}aws acm describe-certificate --certificate-arn {cert_arn} --query 'Certificate.DomainValidationOptions[0].ResourceRecord' --region {region}aws route53 change-resource-record-sets --hosted-zone-id {hosted_zone_id} --change-batch '{"Changes":[{"Action":"UPSERT","ResourceRecordSet":{"Name":"{validation_name}","Type":"CNAME","TTL":300,"ResourceRecords":[{"Value":"{validation_value}"}]}}]}'aws acm wait certificate-validated --certificate-arn {cert_arn} --region {region}aws acm describe-certificate --certificate-arn {cert_arn} --query 'Certificate.Status' --region {region} and retry the wait if status is still PENDING_VALIDATIONConstraints:
scripts/lambda-trust-policy.json. The trust policy includes an aws:SourceAccount condition scoped to the user's account IDACCOUNT_ID placeholder with the actual account ID from Step 1. Use: sed 's/ACCOUNT_ID/{account_id}/' scripts/lambda-trust-policy.json > /tmp/lambda-trust-policy.jsonaws iam create-role --role-name request-authorizer-role --assume-role-policy-document file:///tmp/lambda-trust-policy.jsonaws iam attach-role-policy --role-name request-authorizer-role --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRoleaws iam create-role --role-name example-function-role --assume-role-policy-document file:///tmp/lambda-trust-policy.jsonaws iam attach-role-policy --role-name example-function-role --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRoleConstraints:
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())"aws lambda create-function --function-name request-authorizer --runtime nodejs22.x --handler index.handler --role {authorizer_role_arn} --zip-file fileb:///tmp/authorizer.zip --timeout 10 --region {region}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())"aws lambda create-function --function-name example-function --runtime nodejs22.x --handler index.handler --role {example_role_arn} --zip-file fileb:///tmp/example_function.zip --timeout 10 --region {region}aws lambda get-function --function-name {function_name} --region {region}Constraints:
aws apigateway create-rest-api --name custom-domain-api --endpoint-configuration types=REGIONAL --region {region}aws apigateway get-resources --rest-api-id {api_id} --region {region}aws apigateway create-authorizer --rest-api-id {api_id} --name request-authorizer --type REQUEST --authorizer-uri 'arn:aws:apigateway:{region}:lambda:path/2015-03-31/functions/arn:aws:lambda:{region}:{account_id}:function:request-authorizer/invocations' --identity-source 'method.request.header.HeaderAuth1,method.request.querystring.QueryString1,context.stage' --region {region}aws lambda add-permission --function-name request-authorizer --statement-id apigateway-auth-invoke --action lambda:InvokeFunction --principal apigateway.amazonaws.com --source-arn 'arn:aws:execute-api:{region}:{account_id}:{api_id}/authorizers/{authorizer_id}' --region {region}aws apigateway create-resource --rest-api-id {api_id} --parent-id {root_resource_id} --path-part example --region {region}aws apigateway put-method --rest-api-id {api_id} --resource-id {example_resource_id} --http-method GET --authorization-type CUSTOM --authorizer-id {authorizer_id} --region {region}aws apigateway put-integration --rest-api-id {api_id} --resource-id {example_resource_id} --http-method GET --type AWS_PROXY --integration-http-method POST --uri 'arn:aws:apigateway:{region}:lambda:path/2015-03-31/functions/arn:aws:lambda:{region}:{account_id}:function:example-function/invocations' --region {region}aws lambda add-permission --function-name example-function --statement-id apigateway-invoke --action lambda:InvokeFunction --principal apigateway.amazonaws.com --source-arn 'arn:aws:execute-api:{region}:{account_id}:{api_id}/*/GET/example' --region {region}Constraints:
aws apigateway create-deployment --rest-api-id {api_id} --stage-name {stage_name} --region {region}aws apigateway update-stage --rest-api-id {api_id} --stage-name {stage_name} --patch-operations op=replace,path=/variables/StageVar1,value=stageValue1 --region {region}aws apigateway get-stage --rest-api-id {api_id} --stage-name {stage_name} --region {region} and confirming StageVar1 is present in the variablesaws logs create-log-group --log-group-name api-gw-access-logs --region {region}. Then enable logging with format: aws apigateway update-stage --rest-api-id {api_id} --stage-name {stage_name} --patch-operations op=replace,path=/accessLogSettings/destinationArn,value=arn:aws:logs:{region}:{account_id}:log-group:api-gw-access-logs op=replace,path=/accessLogSettings/format,value='{"requestId":"$context.requestId","ip":"$context.identity.sourceIp","requestTime":"$context.requestTime","httpMethod":"$context.httpMethod","resourcePath":"$context.resourcePath","status":"$context.status"}' --region {region}Constraints:
aws apigateway create-domain-name --domain-name {custom_domain_name} --regional-certificate-arn {acm_certificate_arn} --endpoint-configuration types=REGIONAL --security-policy TLS_1_2 --region {region}aws apigateway create-base-path-mapping --domain-name {custom_domain_name} --rest-api-id {api_id} --stage {stage_name} --base-path '(none)' --region {region}aws apigateway get-domain-name --domain-name {custom_domain_name} --region {region}Constraints:
scripts/dns-record.json with placeholders replaced: sed -e 's/CUSTOM_DOMAIN_NAME/{custom_domain_name}/' -e 's/REGIONAL_DOMAIN_NAME/{regional_domain_name}/' -e 's/REGIONAL_HOSTED_ZONE_ID/{regional_hosted_zone_id}/' scripts/dns-record.json > /tmp/dns-record.jsonaws route53 change-resource-record-sets --hosted-zone-id {hosted_zone_id} --change-batch file:///tmp/dns-record.jsonConstraints:
scripts/validate.sh {custom_domain_name} {api_id} {region} to check all resourcescurl 'https://{custom_domain_name}/example?QueryString1=queryValue1' -H 'HeaderAuth1: headerValue1'{"message": "Hello from the example function!"}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.
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.
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.
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.
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.
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 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}.
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.
headerValue1, queryValue1, stageValue1) in the Lambda authorizer are for demonstration only and are NOT suitable for production. Replace with proper authentication mechanisms (JWT validation, API keys from AWS Secrets Manager, or OAuth) before deploying to production.aws apigateway update-stage --rest-api-id {api_id} --stage-name {stage_name} --patch-operations op=replace,path=/throttle/rateLimit,value=1000 op=replace,path=/throttle/burstLimit,value=2000aws logs associate-kms-key --log-group-name /aws/lambda/request-authorizer --kms-key-arn <KMS_KEY_ARN>aws wafv2 associate-web-acl --web-acl-arn <WAF_ACL_ARN> --resource-arn arn:aws:apigateway:{region}::/restapis/{api_id}/stages/{stage_name}