nice-sundown-31475
06/11/2024, 10:59 PMwhite-balloon-205
nice-sundown-31475
06/12/2024, 11:28 AMnice-sundown-31475
06/12/2024, 11:31 AMnice-sundown-31475
06/12/2024, 11:32 AMnice-sundown-31475
06/12/2024, 11:32 AMtypescript
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// Create an API Gateway REST API
const api = new aws.apigateway.RestApi("myApi", {
description: "My REST API",
});
// Create a resource (e.g., /myresource)
const resource = new aws.apigateway.Resource("myResource", {
parentId: api.rootResourceId,
pathPart: "myresource",
restApi: api.id,
});
// Create a GET method for the resource
const getMethod = new aws.apigateway.Method("getMethod", {
restApi: api.id,
resourceId: resource.id,
httpMethod: "GET",
authorization: "NONE",
});
// Create a mock integration for the GET method
const getIntegration = new aws.apigateway.Integration("getIntegration", {
restApi: api.id,
resourceId: resource.id,
httpMethod: getMethod.httpMethod,
type: "MOCK",
requestTemplates: {
"application/json": `{
"statusCode": 200
}`,
},
integrationResponses: [{
statusCode: "200",
responseTemplates: {
"application/json": `{
"message": "Hello, world!"
}`,
},
}],
});
// Create a method response for the GET method
const getMethodResponse = new aws.apigateway.MethodResponse("getMethodResponse", {
restApi: api.id,
resourceId: resource.id,
httpMethod: getMethod.httpMethod,
statusCode: "200",
responseModels: {
"application/json": "Empty",
},
});
// Enable CORS for the GET method
const corsMethodResponse = new aws.apigateway.MethodResponse("corsMethodResponse", {
restApi: api.id,
resourceId: resource.id,
httpMethod: getMethod.httpMethod,
statusCode: "200",
responseParameters: {
"method.response.header.Access-Control-Allow-Origin": true,
"method.response.header.Access-Control-Allow-Headers": true,
"method.response.header.Access-Control-Allow-Methods": true,
},
});
const corsIntegrationResponse = new aws.apigateway.IntegrationResponse("corsIntegrationResponse", {
restApi: api.id,
resourceId: resource.id,
httpMethod: getMethod.httpMethod,
statusCode: "200",
responseParameters: {
"method.response.header.Access-Control-Allow-Origin": "'*'",
"method.response.header.Access-Control-Allow-Headers": "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'",
"method.response.header.Access-Control-Allow-Methods": "'GET,OPTIONS'",
},
});
// Deploy the API
const deployment = new aws.apigateway.Deployment("deployment", {
restApi: api.id,
stageName: "prod",
}, { dependsOn: [getIntegration, corsIntegrationResponse] });
// Export the URL of the API
export const url = pulumi.interpolate`${deployment.invokeUrl}`;
This program sets up an API Gateway with a single resource and method, and configures CORS headers directly on the method responses. The OPTIONS
method is not handled by a Lambda function but is instead managed by API Gateway's built-in capabilities.nice-sundown-31475
06/12/2024, 11:37 AM// Create a mock integration for the GET method
const getIntegration = new aws.apigateway.Integration("getIntegration", {
restApi: api.id,
resourceId: resource.id,
httpMethod: getMethod.httpMethod,
type: "MOCK",
requestTemplates: {
"application/json": `{
"statusCode": 200
}`,
},
integrationResponses: [{
statusCode: "200",
responseTemplates: {
"application/json": `{
"message": "Hello, world!"
}`,
},
}],
});
integrationResponses
is not a valid property of the IntegrationArgs
object.nice-sundown-31475
06/12/2024, 11:39 AMfull-boots-69133
06/12/2024, 10:28 PM