Skip to content

Instantly share code, notes, and snippets.

@gaffo
Last active October 7, 2018 20:03
Show Gist options
  • Save gaffo/418450aa974fe5ed5edb5352efc55248 to your computer and use it in GitHub Desktop.
Save gaffo/418450aa974fe5ed5edb5352efc55248 to your computer and use it in GitHub Desktop.
CDK Lambda Gateway
import cdk = require('@aws-cdk/cdk');
// import apigateway = require('@aws-cdk/aws-apigateway');
import lambda = require('@aws-cdk/aws-lambda');
// import assets = require('@aws-cdk/assets');
// import s3 = require('@aws-cdk/aws-s3');
import path = require('path');
class MyStack extends cdk.Stack {
constructor(parent: cdk.App, id: string, props?: cdk.StackProps) {
super(parent, id, props);
// const asset =
// new assets.FileAsset(this, 'SampleAsset', {
// path: path.join(__dirname, "python_dir", "index.zip"),
// });
// const bucketRef =
// new s3.Bucket(this, asset.s3BucketName);
// const lmbda =
new lambda.Function(this, "Lambda", {
code: lambda.Code.file(path.join(__dirname, "index.zip")),
handler: 'index.handler',
runtime: lambda.Runtime.Python36,
});
/*
const apigw = new apigateway.LambdaIntegration(lmbda, {
proxy: true,
});
const api = new apigateway.RestApi(this, 'api', {
defaultIntegration: apigw
});
api.root.addMethod('ANY');
const all = api.root.addResource("{proxy+}");
all.addMethod("ANY");
*/
}
}
class MyApp extends cdk.App {
constructor(argv: string[]) {
super(argv);
new MyStack(this, 'hello-cdk', {
env: {
region: "us-west-2",
account: "",
},
});
}
}
process.stdout.write(new MyApp(process.argv).run());
C:\Users\gaffo\cdk-test> cdk deploy hello-cdk -v
Command line arguments: { _: [ 'deploy' ],
trace: false,
strict: false,
'ignore-errors': false,
ignoreErrors: false,
json: false,
j: false,
verbose: true,
v: true,
'version-reporting': undefined,
versionReporting: undefined,
version: false,
help: false,
'$0': '..\\AppData\\Roaming\\npm\\node_modules\\aws-cdk\\bin\\cdk',
app: undefined,
context: undefined,
plugin: undefined,
rename: undefined,
'toolkit-stack-name': undefined,
STACKS: [ 'hello-cdk' ] }
Defaults: {
"app": "node index.js"
}
Setting "aws:cdk:toolkit:default-region" context to undefined
Resolving default credentials
Retrieved account ID 925495567782 from disk cache
Setting "aws:cdk:toolkit:default-account" context to 925495567782
node index.js 'base64:eyJ0eXBlIjoibGlzdCIsImNvbnRleHQiOnsiYXdzOmNkazp0b29sa2l0OmRlZmF1bHQtYWNjb3VudCI6IjkyNTQ5NTU2Nzc4MiJ9fQ=='
Synthesizing hello-cdk
Setting "aws:cdk:toolkit:default-region" context to undefined
Setting "aws:cdk:toolkit:default-account" context to 925495567782
node index.js 'base64:eyJ0eXBlIjoic3ludGgiLCJzdGFja3MiOlsiaGVsbG8tY2RrIl0sImNvbnRleHQiOnsiYXdzOmNkazp0b29sa2l0OmRlZmF1bHQtYWNjb3VudCI6IjkyNTQ5NTU2Nzc4MiJ9fQ=='
Using default AWS SDK credentials for account 925495567782
Waiting for stack CDKToolkit to finish creating or updating...
⏳ Starting deployment of stack hello-cdk...
Preparing assets
- C:\Users\gaffo\cdk-test\index.zip (file)
Preparing asset {"path":"C:\\Users\\gaffo\\cdk-test\\index.zip","id":"hellocdkLambdaCode9A5C6B01","packaging":"file","s3BucketParameter":"LambdaCodeS3Bucket65766E44","s3KeyParameter":"LambdaCodeS3VersionKey10FC11BE"}
Preparing file asset: C:\Users\gaffo\cdk-test\index.zip
Using default AWS SDK credentials for account 925495567782
s3://cdktoolkit-stagingbucket-16wk12z7sevbc/assets/713bbeabd8c320c07ee9f0212d22c0da3db85ed159530dc8e952e61f2fa17c79.zip: checking if already exists
s3://cdktoolkit-stagingbucket-16wk12z7sevbc/assets/713bbeabd8c320c07ee9f0212d22c0da3db85ed159530dc8e952e61f2fa17c79.zip: found (skipping upload)
👑 Asset C:\Users\gaffo\cdk-test\index.zip (file) is up-to-date: s3://cdktoolkit-stagingbucket-16wk12z7sevbc/assets/713bbeabd8c320c07ee9f0212d22c0da3db85ed159530dc8e952e61f2fa17c79.zip
Using default AWS SDK credentials for account 925495567782
Using default AWS SDK credentials for account 925495567782
s3://cdktoolkit-stagingbucket-16wk12z7sevbc/cdk/hello-cdk/aa7fcb10da7853f5eea0c49df1a4b13469b70185cbc96f72e8a509d00ab96ca2.yml: checking if already exists
s3://cdktoolkit-stagingbucket-16wk12z7sevbc/cdk/hello-cdk/aa7fcb10da7853f5eea0c49df1a4b13469b70185cbc96f72e8a509d00ab96ca2.yml: found (skipping upload)
Stored template in S3 at: https://cdktoolkit-stagingbucket-16wk12z7sevbc.s3.amazonaws.com/cdk/hello-cdk/aa7fcb10da7853f5eea0c49df1a4b13469b70185cbc96f72e8a509d00ab96ca2.yml
Stack named hello-cdk already exists, updating it!
Attempting to create ChangeSet CDK-7f8fc3a0-796a-4540-875f-0013ca5d7715 on stack hello-cdk
❌ Deployment of stack hello-cdk failed: ValidationError: Template error: Fn::Select cannot select nonexistent value at index 1
Template error: Fn::Select cannot select nonexistent value at index 1
ValidationError: Template error: Fn::Select cannot select nonexistent value at index 1
at Request.extractError (C:\Users\gaffo\AppData\Roaming\npm\node_modules\aws-cdk\node_modules\aws-sdk\lib\protocol\query.js:47:29)
at Request.callListeners (C:\Users\gaffo\AppData\Roaming\npm\node_modules\aws-cdk\node_modules\aws-sdk\lib\sequential_executor.js:105:20)
at Request.emit (C:\Users\gaffo\AppData\Roaming\npm\node_modules\aws-cdk\node_modules\aws-sdk\lib\sequential_executor.js:77:10)
at Request.emit (C:\Users\gaffo\AppData\Roaming\npm\node_modules\aws-cdk\node_modules\aws-sdk\lib\request.js:683:14)
at Request.transition (C:\Users\gaffo\AppData\Roaming\npm\node_modules\aws-cdk\node_modules\aws-sdk\lib\request.js:22:10)
at AcceptorStateMachine.runTo (C:\Users\gaffo\AppData\Roaming\npm\node_modules\aws-cdk\node_modules\aws-sdk\lib\state_machine.js:14:12)
at C:\Users\gaffo\AppData\Roaming\npm\node_modules\aws-cdk\node_modules\aws-sdk\lib\state_machine.js:26:10
at Request.<anonymous> (C:\Users\gaffo\AppData\Roaming\npm\node_modules\aws-cdk\node_modules\aws-sdk\lib\request.js:38:9)
at Request.<anonymous> (C:\Users\gaffo\AppData\Roaming\npm\node_modules\aws-cdk\node_modules\aws-sdk\lib\request.js:685:12)
at Request.callListeners (C:\Users\gaffo\AppData\Roaming\npm\node_modules\aws-cdk\node_modules\aws-sdk\lib\sequential_executor.js:115:18)
Resources:
LambdaServiceRoleA8ED4D3B:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Statement:
-
Action: 'sts:AssumeRole'
Effect: Allow
Principal:
Service: lambda.amazonaws.com
Version: '2012-10-17'
ManagedPolicyArns:
-
'Fn::Join':
- ""
-
- arn
- ':'
-
Ref: 'AWS::Partition'
- ':'
- iam
- ':'
- ""
- ':'
- aws
- ':'
- policy
- /
- service-role/AWSLambdaBasicExecutionRole
LambdaServiceRoleDefaultPolicyDAE46E21:
Type: 'AWS::IAM::Policy'
Properties:
PolicyDocument:
Statement:
-
Action:
- 's3:GetObject*'
- 's3:GetBucket*'
- 's3:List*'
Effect: Allow
Resource:
-
'Fn::Join':
- ""
-
- arn
- ':'
-
Ref: 'AWS::Partition'
- ':'
- s3
- ':'
- ""
- ':'
- ""
- ':'
-
Ref: LambdaCodeS3Bucket65766E44
-
'Fn::Join':
- ""
-
-
'Fn::Join':
- ""
-
- arn
- ':'
-
Ref: 'AWS::Partition'
- ':'
- s3
- ':'
- ""
- ':'
- ""
- ':'
-
Ref: LambdaCodeS3Bucket65766E44
- /
-
'Fn::Join':
- ""
-
-
'Fn::Select':
- 0
- {'Fn::Split': ['||', {Ref: LambdaCodeS3VersionKey10FC11BE}]}
- '*'
Version: '2012-10-17'
PolicyName: LambdaServiceRoleDefaultPolicyDAE46E21
Roles:
-
Ref: LambdaServiceRoleA8ED4D3B
LambdaD247545B:
Type: 'AWS::Lambda::Function'
Properties:
Code:
S3Bucket:
Ref: LambdaCodeS3Bucket65766E44
S3Key:
'Fn::Join':
- ""
-
-
'Fn::Select':
- 0
-
'Fn::Split':
- '||'
-
Ref: LambdaCodeS3VersionKey10FC11BE
-
'Fn::Select':
- 1
-
'Fn::Split':
- '||'
-
Ref: LambdaCodeS3VersionKey10FC11BE
Handler: index.handler
Role:
'Fn::GetAtt':
- LambdaServiceRoleA8ED4D3B
- Arn
Runtime: python3.6
DependsOn:
- LambdaServiceRoleA8ED4D3B
- LambdaServiceRoleDefaultPolicyDAE46E21
CDKMetadata:
Type: 'AWS::CDK::Metadata'
Properties:
Modules: '@aws-cdk/assets=0.10.0,@aws-cdk/aws-cloudwatch=0.10.0,@aws-cdk/aws-codepipeline-api=0.10.0,@aws-cdk/aws-ec2=0.10.0,@aws-cdk/aws-events=0.10.0,@aws-cdk/aws-iam=0.10.0,@aws-cdk/aws-kms=0.10.0,@aws-cdk/aws-lambda=0.10.0,@aws-cdk/aws-s3=0.10.0,@aws-cdk/aws-s3-notifications=0.10.0,@aws-cdk/aws-sqs=0.10.0,@aws-cdk/cdk=0.10.0,@aws-cdk/cx-api=0.10.0,cdk-test=0.1.0,js-base64=2.4.9'
Parameters:
LambdaCodeS3Bucket65766E44:
Type: String
Description: 'S3 bucket for asset "hello-cdk/Lambda/Code"'
LambdaCodeS3VersionKey10FC11BE:
Type: String
Description: 'S3 key for asset version "hello-cdk/Lambda/Code"'
@gaffo
Copy link
Author

gaffo commented Oct 7, 2018

C:\Users\gaffo\cdk-test> cdk deploy hello-cdk
⏳ Starting deployment of stack hello-cdk...
❌ Deployment of stack hello-cdk failed: ValidationError: Template error: Fn::Select cannot select nonexistent value at index 1
Template error: Fn::Select cannot select nonexistent value at index 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment