Is there a cleaner way to do apigateway.AwsIntegra...
# help
j
Is there a cleaner way to do apigateway.AwsIntegration in SST? This works, but I'm wondering if there's a better way, leveraging SST.
Copy code
import * 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' }] });
  }
}
f
Hey @Jon Holman, we don’t currently have higher level support for AWS Integration in SST.
j
Awesome, thanks @Frank