Hey there, just wanted to drop a note with a problem I ran into and how I fixed it, considering Google has zero hits for this particular error.
Given I am hosting a DB in AWS RDS, I wanted to deploy the lambda function to a VPC with connectivity to it, whitelisting inbound traffic from the VPC it's attached to. I got the VPC attachment figured out by adding the following config to the lambdaFunction
const in appsync.ts
:
vpc: ec2.Vpc.fromLookup(this, "default_vpc", {vpcId: "<VPC_ID>"}),
allowPublicSubnet: true,
allowAllOutbound: true,
However, when CDK tried to deploy it, it runs into an issue where it cannot create network interfaces.
In order to fix this, the correct policy has to be added to the lambda execution role. Conveniently, the creators of this package added a policies
field in the AppSyncStackProps.function
to facilitate this.
HOWEVER, this interface doesn't work properly, as it is passing raw objects to new iamPolicyDocument
here. The statement
prop this is being passed to expects PolicyStatement[];
-- I believe the fact the AppSyncStackProps.function.policies
field lacking a type results in this finding the following weird runtime error: resolution error: statement.freeze is not a function.
.
What I discovered is that the CDK boilerplate needs to be creating new iam.PolicyStatement
and pass that to statements
like so:
inlinePolicies: {
customApiFunctionPolicy: new iam.PolicyDocument({
statements: this.props.function.policies.map((statement) => {
return new iam.PolicyStatement(statement)
})
})
},
My parameters to AppSyncStackProps
in index.ts
now looks like this:
function: {
code: join(process.cwd(), 'handler.ts'),
policies: [
{
actions: [
"ec2:DescribeNetworkInterfaces",
"ec2:CreateNetworkInterface",
"ec2:DeleteNetworkInterface",
"ec2:DescribeInstances",
"ec2:AttachNetworkInterface"
],
resources: ["*"]
}
],
After implementing this cdk synth
now outputs a lambda execution role with the proper permissions and deployment succeeds.
I don't have time at the moment to make a PR for this, but like I said, figured this might be helpful to someone else down the line.
Keywords:
resolution error: statement.freeze is not a function.