简体   繁体   中英

AWS AppConfig Validation Lambda Policy in SAM Template

I'm trying to add a policy to a lambda to allow AppConfig to invoke it. I can do this through the terminal using this command:

aws lambda add-permission --function-name ConfigValidator.Arn --action lambda:InvokeFunction --statement-id appconfig --principal appconfig.amazonaws.com --output json --region eu-west-1

But how can this be done automatically through the SAM template?

Here is how I do this:

  1. Create a managed policy with access to your AppConfig
  2. Attach that managed policy to the role your lambda is configured to use

Here is the code using CDK (CDK is the latest and greatest tool to create AWS resources, I highly recommend using it!).

If you don't want to use CDK you can manually setup the same managed policies by hand.

Detailed example below:

Create a managed policy with access to your AppConfig

const resourceArn = `arn:aws:appconfig:${props.region}:${props.accountId}:application/${this.appConfigApplication.ref}*`
this.appConfigReaderManagedPolicy = new ManagedPolicy(this, `AppConfigReader-${id}`, {
    managedPolicyName: `AppConfigReader-${id}`,
    description: `Readonly access to ${id}`,
    statements: [
        new PolicyStatement({
            resources: [resourceArn],
            actions: [
                'appconfig:GetConfiguration',
                'appconfig:GetApplication',
            ]
        })
    ]
})

Attach that managed policy to the role your lambda is configured to use

//assuming your lambda is already configured somewhere

this.lambdaFunction.role.addManagedPolicy(this.appConfigReaderManagedPolicy)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM