snap40 / serverless-aws-cdk Goto Github PK
View Code? Open in Web Editor NEWServerless Plugin to enable defining infrastructure with AWS CDK
License: Apache License 2.0
Serverless Plugin to enable defining infrastructure with AWS CDK
License: Apache License 2.0
Trying to deploy now, I have AWS credentials configured but CDK says it can't load them
Error: Need to perform AWS calls for account 1234xxxxxx, but no credentials have been configured.
at SdkProvider.obtainCredentials (/Users/cyber/dev/jb/mercury/packages/backend/node_modules/aws-cdk/lib/api/aws-auth/sdk-provider.ts:245:11)
at SdkProvider.forEnvironment (/Users/cyber/dev/jb/mercury/packages/backend/node_modules/aws-cdk/lib/api/aws-auth/sdk-provider.ts:131:19)
at Function.lookup (/Users/cyber/dev/jb/mercury/packages/backend/node_modules/aws-cdk/lib/api/bootstrap/deploy-bootstrap.ts:35:17)
at Bootstrapper.modernBootstrap (/Users/cyber/dev/jb/mercury/packages/backend/node_modules/aws-cdk/lib/api/bootstrap/bootstrap-environment.ts:81:21)
at AwsCdkDeploy.bootstrapToolkitStack (/Users/cyber/dev/jb/mercury/packages/backend/node_modules/serverless-aws-cdk/cdk/toolkitStack.ts:17:3)
After installing this plugin with yarn
, I encountered this bug when running serverless deploy
:
$ serverless deploy
Serverless: Running "serverless" installed locally (in service node_modules)
Serverless: DOTENV: Loading environment variables from :
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Type Error ---------------------------------------------
TypeError: aws_cdk_1.bootstrapEnvironment is not a function
at AwsCdkDeploy.bootstrapToolkitStack (/Users/andrew/LocusLabs/server/analytics/analytics-api/packages/analytics-intake-api/node_modules/serverless-aws-cdk/cdk/toolkitStack.ts:17:9)
Cloning this repo, I noticed that different versions of the aws-cdk get installed when installing with npm
vs yarn
.
Here's a Code Poiner to the line causing the issue. I think the fix is a matter of instantiating a new Bootstrapper
and calling the bootstrapEnvironment
method on the instance.
functions:
sendTestSmsApi:
handler: src/api/TwilioApi.sendTestSmsHandler
export class Api extends api.InfrastructureConstruct {
api: HttpApi
addFunctionRoute(path: string, methods: HttpMethod[], handler: lambda.Function): void {
this.api.addRoutes({
path,
methods,
integration: new LambdaProxyIntegration({
payloadFormatVersion: PayloadFormatVersion.VERSION_2_0, // v2 payload
handler,
}),
})
}
constructor(scope: cdk.Construct, id: string, props: api.InfrastructureProps) {
super(scope, id, props)
// our HTTP API Gateway
this.api = new HttpApi(this, "API", {
corsPreflight: { allowOrigins: ["*"] }, // CORS
createDefaultStage: true,
})
// mapping of routes to functions
// sadly this is needed on account of https://github.com/snap40/serverless-aws-cdk/issues/15#issuecomment-706307080
// we should improve serverless-aws-cdk to do this automatically based on serverless.yml
this.addFunctionRoute("/testSms", [HttpMethod.POST], props.functions.sendTestSmsApi)
// this.addFunctionRoute("/testSms", [HttpMethod.POST], props.functions.sendTestSms)
new CfnOutput(this, "APIGW", {
description: "API Gateway Base URL",
value: this.api.url!,
})
}
}
"errorType": "Runtime.ImportModuleError", "errorMessage": "Error: Cannot find module 'TwilioApi'\nRequire stack:\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js", "stack": [ "Runtime.ImportModuleError: Error: Cannot find module 'Twilio'", "Require stack:", "- /var/runtime/UserFunction.js", "- /var/runtime/index.js", " at _loadUserApp (/var/runtime/UserFunction.js:100:13)", " at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)", " at Object.<anonymous> (/var/runtime/index.js:43:30)", " at Module._compile (internal/modules/cjs/loader.js:1137:30)", " at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)", " at Module.load (internal/modules/cjs/loader.js:985:32)", " at Function.Module._load (internal/modules/cjs/loader.js:878:14)", " at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)", " at internal/main/run_main_module.js:17:47" ] }
--
AWS_PROFILE
should be set based on my provider.profile
configuration
Normal AWS provider outputs APIGW base URL and endpoint URLs. It's very helpful to have after doing a new deployment. Would be nice to have.
import { api } from "serverless-aws-cdk"
import { CfnOutput, Construct, Duration } from "@aws-cdk/core"
import { Vpc, SubnetType } from "@aws-cdk/aws-ec2"
import {
ServerlessCluster,
ParameterGroup,
DatabaseClusterEngine,
SubnetGroup,
AuroraCapacityUnit,
} from "@aws-cdk/aws-rds"
import * as cdk from "@aws-cdk/core"
import { Secret } from "@aws-cdk/aws-secretsmanager"
export class AuroraSlsStack extends api.InfrastructureConstruct {
constructor(scope: cdk.Construct, id: string, props: api.InfrastructureProps) {
super(scope, id, props)
const self = (this as unknown) as Construct // otherwise type checks fail with `this`
// create vpc
const vpc = new Vpc(self, "Vpc", {
cidr: "10.99.0.0/16",
natGateways: 0,
subnetConfiguration: [{ name: "aurora_isolated_", subnetType: SubnetType.ISOLATED }],
})
// get subnetids from vpc
const subnetIds: string[] = []
vpc.isolatedSubnets.forEach((subnet) => {
subnetIds.push(subnet.subnetId)
})
// output subnet ids
new CfnOutput(self, "VpcSubnetIds", {
value: JSON.stringify(subnetIds),
})
// output security group
new CfnOutput(self, "VpcDefaultSecurityGroup", {
value: vpc.vpcDefaultSecurityGroup,
})
// create subnetgroup
const dbSubnetGroup = new SubnetGroup(self, "AuroraSubnetGroup", {
description: "Subnet group to access aurora",
vpc: vpc,
vpcSubnets: vpc.selectSubnets({
subnetType: SubnetType.PRIVATE,
}),
})
// create secret
const dbSecret = new Secret(self, "AuroraDBSecret", {
secretName: "/" + [props.stackName, "aurora"].join("/"),
generateSecretString: {
generateStringKey: "password",
secretStringTemplate: '{"username": "dbadmin"}',
passwordLength: 20,
excludeCharacters: " %+:;{}",
},
})
// create aurora db serverless cluster
const aurora = new ServerlessCluster(self, "MercuryAuroraDB", {
deletionProtection: false, // change me!
clusterIdentifier: "mercury",
defaultDatabaseName: "mercury",
engine: DatabaseClusterEngine.AURORA_POSTGRESQL,
subnetGroup: dbSubnetGroup,
enableHttpEndpoint: true,
parameterGroup: ParameterGroup.fromParameterGroupName(self, "ParameterGroup", "default.aurora-postgresql10"),
scaling: {
autoPause: Duration.days(4),
minCapacity: AuroraCapacityUnit.ACU_2,
maxCapacity: AuroraCapacityUnit.ACU_8,
},
vpc: vpc,
})
// const secretDBAttachment = new secretsmanager.SecretTargetAttachment(this, "MercuryDBSecretAttachment", {
// secret: dbSecret,
// target: aurora.asSecretAttachmentTarget(),
// })
//wait for subnet group to be created
// aurora.addDependsOn(dbSubnetGroup)
// construct arn from available information
const account = props.env?.account
const region = props.env?.region
new CfnOutput(self, "AuroraClusterArn", {
exportName: `${props.env}`,
value: `arn:aws:rds:${region}:${account}:cluster:${aurora.clusterIdentifier}`,
})
new CfnOutput(self, "AuroraSecretArn", {
exportName: `${props.env}`,
value: dbSecret.secretArn,
})
}
}
Serverless: Compiling TypeScript infrastructure definition with config /Users/cyber/dev/jb/mercury/packages/backend/tsconfig.json...
../../../serverless-aws-cdk/provider/awsCdkProvider.ts(2,29): error TS7016: Could not find a declaration file for module 'serverless'. '/Users/cyber/dev/jb/serverless-aws-cdk/node_modules/serverless/lib/Serverless.js' implicitly has an 'any' type.
Try `npm install @types/serverless` if it exists or add a new declaration (.d.ts) file containing `declare module 'serverless';`
../../../serverless-aws-cdk/cdk/toolkitStack.ts(1,29): error TS7016: Could not find a declaration file for module 'serverless'. '/Users/cyber/dev/jb/serverless-aws-cdk/node_modules/serverless/lib/Serverless.js' implicitly has an 'any' type.
Try `npm install @types/serverless` if it exists or add a new declaration (.d.ts) file containing `declare module 'serverless';`
../../../serverless-aws-cdk/deploy/awsCdkDeploy.ts(2,29): error TS7016: Could not find a declaration file for module 'serverless'. '/Users/cyber/dev/jb/serverless-aws-cdk/node_modules/serverless/lib/Serverless.js' implicitly has an 'any' type.
Try `npm install @types/serverless` if it exists or add a new declaration (.d.ts) file containing `declare module 'serverless';`
../../../serverless-aws-cdk/remove/awsCdkRemove.ts(2,29): error TS7016: Could not find a declaration file for module 'serverless'. '/Users/cyber/dev/jb/serverless-aws-cdk/node_modules/serverless/lib/Serverless.js' implicitly has an 'any' type.
Try `npm install @types/serverless` if it exists or add a new declaration (.d.ts) file containing `declare module 'serverless';`
../../../serverless-aws-cdk/diff/awsCdkDiff.ts(2,29): error TS7016: Could not find a declaration file for module 'serverless'. '/Users/cyber/dev/jb/serverless-aws-cdk/node_modules/serverless/lib/Serverless.js' implicitly has an 'any' type.
Try `npm install @types/serverless` if it exists or add a new declaration (.d.ts) file containing `declare module 'serverless';`
../../../serverless-aws-cdk/compile/awsCdkCompile.ts(2,29): error TS7016: Could not find a declaration file for module 'serverless'. '/Users/cyber/dev/jb/serverless-aws-cdk/node_modules/serverless/lib/Serverless.js' implicitly has an 'any' type.
Try `npm install @types/serverless` if it exists or add a new declaration (.d.ts) file containing `declare module 'serverless';`
cdk/index.ts(7,11): error TS2345: Argument of type 'import("/Users/cyber/dev/jb/mercury/packages/backend/node_modules/@aws-cdk/core/lib/construct-compat").Construct' is not assignable to parameter of type 'import("/Users/cyber/dev/jb/serverless-aws-cdk/node_modules/@aws-cdk/core/lib/construct-compat").Construct'.
Types of property 'node' are incompatible.
Type 'import("/Users/cyber/dev/jb/mercury/packages/backend/node_modules/@aws-cdk/core/lib/construct-compat").ConstructNode' is not assignable to type 'import("/Users/cyber/dev/jb/serverless-aws-cdk/node_modules/@aws-cdk/core/lib/construct-compat").ConstructNode'.
Types have separate declarations of a private property 'host'.
cdk/index.ts(9,24): error TS2345: Argument of type 'this' is not assignable to parameter of type 'Construct'.
Type 'Infrastructure' is not assignable to type 'Construct'.
Types of property 'node' are incompatible.
Type 'import("/Users/cyber/dev/jb/serverless-aws-cdk/node_modules/@aws-cdk/core/lib/construct-compat").ConstructNode' is not assignable to type 'import("/Users/cyber/dev/jb/mercury/packages/backend/node_modules/@aws-cdk/core/lib/construct-compat").ConstructNode'.
Types have separate declarations of a private property 'host'.
cdk1/database.ts(16,11): error TS2345: Argument of type 'import("/Users/cyber/dev/jb/mercury/packages/backend/node_modules/@aws-cdk/core/lib/construct-compat").Construct' is not assignable to parameter of type 'import("/Users/cyber/dev/jb/serverless-aws-cdk/node_modules/@aws-cdk/core/lib/construct-compat").Construct'.
cdk1/database.ts(53,33): error TS2345: Argument of type 'this' is not assignable to parameter of type 'Construct'.
Type 'AuroraSlsStack' is not assignable to type 'Construct'.
Property 'onValidate' is protected but type 'Construct' is not a class derived from 'Construct'.
cdk1/database.ts(71,61): error TS2345: Argument of type 'this' is not assignable to parameter of type 'Construct'.
Error --------------------------------------------------
Error: TypeScript compilation failed
at AwsCdkCompile.compile (/Users/cyber/dev/jb/serverless-aws-cdk/cdk/compile.ts:11:15)
at AwsCdkCompile.tryCatcher (/Users/cyber/dev/jb/serverless-aws-cdk/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/Users/cyber/dev/jb/serverless-aws-cdk/node_modules/bluebird/js/release/promise.js:547:31)
at Promise._settlePromise (/Users/cyber/dev/jb/serverless-aws-cdk/node_modules/bluebird/js/release/promise.js:604:18)
at Promise._settlePromiseCtx (/Users/cyber/dev/jb/serverless-aws-cdk/node_modules/bluebird/js/release/promise.js:641:10)
at _drainQueueStep (/Users/cyber/dev/jb/serverless-aws-cdk/node_modules/bluebird/js/release/async.js:97:12)
at _drainQueue (/Users/cyber/dev/jb/serverless-aws-cdk/node_modules/bluebird/js/release/async.js:86:9)
at Async._drainQueues (/Users/cyber/dev/jb/serverless-aws-cdk/node_modules/bluebird/js/release/async.js:102:5)
at Immediate.Async.drainQueues [as _onImmediate] (/Users/cyber/dev/jb/serverless-aws-cdk/node_modules/bluebird/js/release/async.js:15:14)
at processImmediate (internal/timers.js:456:21)
at process.topLevelDomainCallback (domain.js:137:15)
Trying to use this plugin results in the following error.
TypeError: Cannot read property 'Outputs' of undefined
at Object.<anonymous> (/Users/jim/Code/foo/node_modules/serverless-plugin-datadog/src/output.ts:15:78)
at Generator.next (<anonymous>)
at fulfilled (/Users/jim/Code/foo/node_modules/serverless-plugin-datadog/dist/src/output.js:5:58)
For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.
When using this module the "serverless package" command doesn't produce the CloudFormation template output.
I've not tested yet but that means either "serverless deploy --package $package_dir" doesn't work or will regenerate the CloudFormation template again on deploy. This isn't ideal, one point of using package is so that you have completely static content for a given deploy you can always rollback to.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.