Create an aws-apigateway.RestAPI resource with mock for CORS header
YAMLTo set up an AWS API Gateway REST API with a mock for CORS headers, you will need to create a new
aws.apigateway.RestApi
, define aaws.apigateway.Resource
for the OPTIONS method, and configure aaws.apigateway.MethodResponse
andaws.apigateway.IntegrationResponse
to include the necessary CORS headers. Below is a Pulumi YAML program that specifies these resources:name: aws-apigateway-cors-mock runtime: yaml resources: # Create a new REST API myApi: type: aws:apigateway:RestApi properties: name: "MyDemoAPI" # This is the link to the resource's documentation: # https://www.pulumi.com/registry/packages/aws/api-docs/apigateway/restapi/ # Define a root resource ('/') to attach methods rootResource: type: aws:apigateway:Resource properties: restApi: ${myApi.id} parentId: ${myApi.rootResourceId} pathPart: "" # This is the link to the resource's documentation: # https://www.pulumi.com/registry/packages/aws/api-docs/apigateway/resource/ # Create an OPTIONS method for the root resource to handle CORS preflight requests optionsMethod: type: aws:apigateway:Method properties: restApi: ${myApi.id} resourceId: ${rootResource.id} httpMethod: "OPTIONS" authorization: "NONE" # This is the link to the resource's documentation: # https://www.pulumi.com/registry/packages/aws/api-docs/apigateway/method/ # Set the mock integration for OPTIONS method mockIntegration: type: aws:apigateway:Integration properties: restApi: ${myApi.id} resourceId: ${rootResource.id} httpMethod: ${optionsMethod.httpMethod} type: "MOCK" # Indicates this is a mock integration requestTemplates: "application/json": "{\"statusCode\": 200}" # A simple request template # This is the link to the resource's documentation: # https://www.pulumi.com/registry/packages/aws/api-docs/apigateway/integration/ # Define the method response for 200 methodResponse200: type: aws:apigateway:MethodResponse properties: restApi: ${myApi.id} resourceId: ${rootResource.id} httpMethod: ${optionsMethod.httpMethod} statusCode: "200" responseModels: {"application/json": "Empty"} # This is the link to the resource's documentation: # https://www.pulumi.com/registry/packages/aws/api-docs/apigateway/methodresponse/ # Define the integration response to include CORS headers integrationResponse200: type: aws:apigateway:IntegrationResponse properties: restApi: ${myApi.id} resourceId: ${rootResource.id} httpMethod: ${optionsMethod.httpMethod} statusCode: "200" responseTemplates: {"application/json": ""} responseParameters: "method.response.header.Access-Control-Allow-Headers": "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,Access-Control-Allow-Origin'" "method.response.header.Access-Control-Allow-Methods": "'DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT'" "method.response.header.Access-Control-Allow-Origin": "'*'" # This is the link to the resource's documentation: # https://www.pulumi.com/registry/packages/aws/api-docs/apigateway/integrationresponse/
This program sets up a REST API with a mock integration that responds to OPTIONS preflight requests for CORS by providing the necessary headers such as
Access-Control-Allow-Origin
,Access-Control-Allow-Methods
, andAccess-Control-Allow-Headers
.Remember that after setting up the API Gateway resources, you will also need to deploy the API for it to become active. This usually involves creating an
aws.apigateway.Deployment
and possibly linking it to aaws.apigateway.Stage
. This part is not included in the provided code, but you should take it as the next step to actually deploy and utilize your API.