Jon Holman
01/18/2022, 11:39 PMimport * as sst from "@serverless-stack/resources";
import { RemovalPolicy, Aws } from "aws-cdk-lib";
import * as apigateway from 'aws-cdk-lib/aws-apigateway';
import * as iam from 'aws-cdk-lib/aws-iam';
export default class PublicBlogStack extends sst.Stack {
constructor(scope, id, props) {
super(scope, id, props);
const { table } = props;
const apiRole = new iam.Role(this, 'ApiRole', {
assumedBy: new iam.ServicePrincipal('<http://apigateway.amazonaws.com|apigateway.amazonaws.com>'),
});
apiRole.addToPolicy(new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
resources: [`arn:aws:dynamodb:${Aws.REGION}:${Aws.ACCOUNT_ID}:table/${table.tableName}`],
actions: ["dynamodb:Scan"]
}))
const api = new apigateway.RestApi(this, 'ServerlessNotesApi', {});
const notesListIntegration = new apigateway.AwsIntegration({
service: 'dynamodb',
action: 'Scan',
options: {
credentialsRole: apiRole,
requestTemplates: { 'application/json': `{"TableName": "${table.tableName}"}`, },
integrationResponses: [
{
statusCode: '200',
responseTemplates: {
'application/json': `#set($inputRoot = $input.path('$'))
{
"notes": [
#foreach($elem in $inputRoot.Items) {
"noteId": "$elem.noteId.S",
"content": "$elem.content.S"
}#if($foreach.hasNext),#end
#end
]
}`,
},
},
],
},
});
api.root.addMethod('GET', notesListIntegration, { methodResponses: [{ statusCode: '200' }] });
}
}
Frank
Frank
Jon Holman
01/19/2022, 6:36 PM