The docs are very clear in doing this in the console, https://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-s3.html, but replicating in CDK is really painful.
My question is how to create an Rest API backed with s3 (no lambda in the middle) in CDK, or at least how the apigateway.AwsIntegration works.
I've tried many things and realized that I was coding blind. I have made already multiple integrations between restApi, lambda, sqs, dynamoDB, s3 and all of them were so easy. But integrating API Gateway with S3 directly is about to make me cry.
I need that a restAPI store the request payload directly to an S3 bucket.
This is what I already tried:
Add a policy stament to RestApi:
const apiResourcePolicy = new iam.PolicyDocument({
statements: [
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: ['s3:Put*'],
resources: [bucket.bucketName],
principals: [new ServicePrincipal('apigateway.amazonaws.com')]
})
]
});
const api = new apigateway.RestApi(this, "dispatch-api", {
restApiName: "my api name",
description: "api description",
policy: apiResourcePolicy
});
I thought that this should be enough to solve permissions issues, but when I added the AWS Integration to the API, like this:
const getS3Integration = new apigateway.AwsIntegration({
service: "s3",
path: bucket.bucketName,
});
api.root.addMethod("PUT", getS3Integration, {
requestValidator: requestValidator,
requestModels: {"application/json": myModel},
});
I'm getting:
1:41:11 PM | CREATE_FAILED | AWS::ApiGateway::Method | XXXXXXXXXXXXXXXX
Role ARN must be specified for AWS integrations (Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException; Request ID: xxxxxxxxxxxxxxxxxxxxx; Proxy:
null)
I don't know how to specify that Role ARN, I did not found that in the docs. Don't know either if add policy to restAPI was necessary. I was trying to replicate the example here in CDK.