appconfig.yaml
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: "AWS AppConfig extension for Lambda Example"

Parameters

These are the input parameters for this template. All of these parameters must be supplied for this template to be deployed.

Parameters:

Number of exclamation points

ExclamationPoints: Description: Name to be used for the EventBridge Bus and API Gateway, i.e. some-service-name Type: String ServiceName: Description: Name for this deployed service Type: String Default: helloworld-example-app Mappings:

Values for the App Config Extension ARN can be found at: https://docs.aws.amazon.com/appconfig/latest/userguide/appconfig-integration-lambda-extensions.html These were the versions as of Nov 2020

RegionMap: us-east-1: "appconfiglayerarn": "arn:aws:lambda:us-east-1:027255383542:layer:AWS-AppConfig-Extension:11" us-west-1: "appconfiglayerarn": "arn:aws:lambda:us-west-1:958113053741:layer:AWS-AppConfig-Extension:11" us-east-2: "appconfiglayerarn": "arn:aws:lambda:us-east-2:728743619870:layer:AWS-AppConfig-Extension:15" us-west-2: "appconfiglayerarn": "arn:aws:lambda:us-west-2:359756378197:layer:AWS-AppConfig-Extension:18" Resources: AppConfig: Type: AWS::AppConfig::Application Properties: Description: !Sub "${ServiceName} configuration example" Name: !Ref ServiceName DevEnvironmentConfig: Type: AWS::AppConfig::Environment Properties: ApplicationId: !Ref AppConfig Name: dev ConfigProfile: Type: AWS::AppConfig::ConfigurationProfile Properties: ApplicationId: !Ref AppConfig LocationUri: hosted Name: ExclamationPoints DevConfiguration: Type: AWS::AppConfig::HostedConfigurationVersion Properties: ApplicationId: !Ref AppConfig ConfigurationProfileId: !Ref ConfigProfile Description: !Sub "${ServiceName}-dev-default" ContentType: 'application/json' Content: | { "enableExclamationPoints": true, "numberOfExclamationPoints": 5 } DevConfigDeployment: Type: AWS::AppConfig::Deployment Properties: ApplicationId: !Ref AppConfig EnvironmentId: !Ref DevEnvironmentConfig DeploymentStrategyId: AppConfig.AllAtOnce ConfigurationProfileId: !Ref ConfigProfile ConfigurationVersion: '1' Description: !Sub "${ServiceName}-dev-deployment"

Hello World Lambda Function

Create a very basic Lambda function that just says hello world with some number of configurable excalmation points.

LambdaFunction: Type: AWS::Lambda::Function Properties: Handler: index.handler Description: HelloWorld AppConfig Function Role: !GetAtt LambdaExecutionRole.Arn Runtime: python3.8 Layers: - !FindInMap [ RegionMap, !Ref "AWS::Region" , appconfiglayerarn ] Timeout: 60 Code: ZipFile: !Sub | import json import urllib.request def handler(event, context): num_exclamations = get_configured_number_of("ExclamationPoints", 1) return f'Hello world{"!" * num_exclamations}' def get_configured_number_of(configuration_type, default): try: url = f'http://localhost:2772/applications/${ServiceName}/environments/dev/configurations/' + configuration_type config = json.loads(urllib.request.urlopen(url).read()) if config.get("enable" + configuration_type, False): return config.get("numberOf" + configuration_type, default) else: return default except: return default LambdaExecutionRole: Type: 'AWS::IAM::Role' Properties: AssumeRolePolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Principal: Service: - lambda.amazonaws.com Action: - 'sts:AssumeRole' Path: / Policies: - PolicyName: LambdaExecutionRole PolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Action: - appconfig:ListTagsForResource - appconfig:GetEnvironment - appconfig:GetDeploymentStrategy - appconfig:GetHostedConfigurationVersion - appconfig:GetDeployment - appconfig:GetConfiguration - appconfig:GetApplication - appconfig:GetConfigurationProfile Resource: - !Sub "arn:aws:appconfig:${AWS::Region}:${AWS::AccountId}:application/${AppConfig}" - !Sub "arn:aws:appconfig:${AWS::Region}:${AWS::AccountId}:application/${AppConfig}/*"