Nicola Brisotto
03/23/2021, 10:51 PMsst start
but that way I have to hit the API Gateway manually (or with a custom automation). So, as far as I can see, there isn’t a 1 to 1 alternative to serverless invoke local
, am I right?Frank
sst start
, the IAM credentials from the real Lambda function are passed down to ur local, and the local function is executed using it.Frank
Frank
sst start
is not running? (ie. sst start takes too long to startup)Mike McCall
03/23/2021, 11:54 PMFrank
cdk synth
run sst build
and you can find the template yaml in .build/cdk.out
Mike McCall
03/23/2021, 11:59 PMnpx sst cdk --app=build/run.js synth --no-staging > template.yaml
Frank
Frank
Mike McCall
03/24/2021, 12:03 AMPreparing your SST app
Detected tsconfig.json
Transpiling source
Linting source
Running type checker
...
Might be worth add a -q flag to the cli?Nicola Brisotto
03/24/2021, 4:34 PMserverless invoke local
is very useful to quickly iterate on you code: mock the data once and with one command you can run your code multiple time.Nicola Brisotto
03/24/2021, 4:37 PMsst start
and add some utily to trigger the lambda execution with real data sent to the API Gateway. But then you have the problem that not all lambda are invoked by the API GW (see lambda and SNS for example https://docs.aws.amazon.com/lambda/latest/dg/with-sns.html)Frank
sst start
Nicola Brisotto
03/24/2021, 6:46 PMaws lambda invoke --function-name <FUNCTION_ARN> --payload "$(cat mocks/topic-create-event.json)" response.json
It’s not handy like serverless invoke (you have to find manually the lambda ARN). But it do the job 🙂Mike McCall
03/24/2021, 6:51 PMsam local generate-event [OPTIONS] COMMAND [ARGS]...
https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-local-generate-event.htmlNicola Brisotto
03/24/2021, 7:37 PMnpx sst cdk --app=build/run.js synth --no-staging > template.yaml
produce this template.yaml
:
Preparing your SST app
Transpiling source
Linting source
Nicola Brisotto
03/24/2021, 7:47 PMsst build
generate a json template how is it possible to switch to it to yaml ?Frank
--json
flagNicola Brisotto
03/24/2021, 7:59 PMnpx sst build --yaml
but it still generate a json .build/cdk.out/dev-my-sst-app-my-stack.template.json
Frank
Frank
Nicola Brisotto
03/24/2021, 8:13 PMsam
require a template.ymlFrank
json2yaml template.json > template.yaml
?
https://github.com/coolaj86/json2yaml#usageFrank
sam
command r u using just curious?Nicola Brisotto
03/24/2021, 8:20 PMFrank
sam
?Nicola Brisotto
03/24/2021, 8:21 PMMike McCall
03/24/2021, 8:22 PMMike McCall
03/24/2021, 8:22 PMMike McCall
03/24/2021, 8:24 PMNicola Brisotto
03/24/2021, 8:25 PMNicola Brisotto
03/24/2021, 8:26 PMnpx create-serverless-stack@latest my-sst-app
Mike McCall
03/24/2021, 8:26 PMMike McCall
03/24/2021, 8:27 PMFrank
cdk synth
is a bit weird, u need to give it a stack name for it to print out the template, npx sst cdk --app=build/run.js synth dev-my-sst-app-my-stack > template.yaml
Mike McCall
03/24/2021, 8:30 PMSupply a stack id (…)
and list the stacksNicola Brisotto
03/24/2021, 8:35 PMMike McCall
03/24/2021, 8:38 PMMike McCall
03/24/2021, 8:38 PMnpx sst cdk --app=build/run.js ls
Mike McCall
03/24/2021, 8:38 PMNicola Brisotto
03/24/2021, 8:41 PMnpx sst cdk --app=build/run.js ls
Preparing your SST app
Transpiling source
Linting source
Nicola Brisotto
03/24/2021, 8:42 PMnpx create-serverless-stack@latest my-sst-app
Mike McCall
03/24/2021, 8:42 PMFrank
.build/run.js
instead of build/run.js
Frank
npx sst cdk --app=.build/run.js ls
Nicola Brisotto
03/24/2021, 8:47 PMnpx sst cdk --app=.build/run.js ls
Preparing your SST app
Transpiling source
Linting source
Mike McCall
03/24/2021, 8:48 PMFrank
Mike McCall
03/24/2021, 8:49 PMMike McCall
03/24/2021, 8:51 PMMike McCall
03/24/2021, 8:56 PM{
"app": ".build/run.js"
}
and run any cdk command against it.
$ cdk synth
Mike McCall
03/24/2021, 8:59 PMcdk synth --app=.build/run.js --no-staging > template.yaml
Nicola Brisotto
03/24/2021, 9:04 PMFrank
Mike McCall
03/24/2021, 9:07 PM--app=.build/run.js
does appear to work on both.Mike McCall
03/24/2021, 9:10 PMnpx sst
. Let me know if I can help with anything.Frank
cdk synth
And this does not work:
npx sst cdk synth
I’m working on a fixFrank
Frank
npx sst cdk synth
No need to pass in --app
, that’s all hooked up internallyFrank
npx sst cdk synth
and that will print out the CF template. If you have multiple stacks, pass in the stack you want printed ie. npx sst cdk synth STACK
Frank
Nicola Brisotto
03/26/2021, 12:59 PMlib/index.js
import MyStack from "./MyStack";
export default function main(app) {
new MyStack(app, "my-stack");
new MyStack(app, "my-stack2"); //ADD this and the yml out is gone
}
Frank
Frank
npx sst cdk synth STACK
Nicola Brisotto
03/26/2021, 1:15 PMnpx sst cdk synth STACK
prints out the stack only if you have NO dependency on other stacks. EX: npx sst cdk synth dev-course-manager-sst-api
Including dependency stacks: dev-course-manager-sst-dynamodb
Successfully synthesized to /Users/nicolabrisotto/SRC/SERVERLESS/course-manager/service/cdk.out
Nicola Brisotto
03/26/2021, 1:28 PMsam
I suppose because it expect a function defined as Type: AWS::Serverless::Function
Mike McCall
03/26/2021, 3:25 PMNicola Brisotto
03/26/2021, 6:37 PM2021-03-26 19:34:52 Loading policies from IAM...
2021-03-26 19:34:57 Finished loading policies from IAM.
Traceback (most recent call last):
File "/usr/local/bin/sam", line 8, in <module>
sys.exit(cli())
File "/usr/local/Cellar/aws-sam-cli/1.21.1/libexec/lib/python3.8/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/usr/local/Cellar/aws-sam-cli/1.21.1/libexec/lib/python3.8/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/usr/local/Cellar/aws-sam-cli/1.21.1/libexec/lib/python3.8/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/local/Cellar/aws-sam-cli/1.21.1/libexec/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/Cellar/aws-sam-cli/1.21.1/libexec/lib/python3.8/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/usr/local/Cellar/aws-sam-cli/1.21.1/libexec/lib/python3.8/site-packages/click/decorators.py", line 73, in new_func
return ctx.invoke(f, obj, *args, **kwargs)
File "/usr/local/Cellar/aws-sam-cli/1.21.1/libexec/lib/python3.8/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/usr/local/Cellar/aws-sam-cli/1.21.1/libexec/lib/python3.8/site-packages/samcli/lib/telemetry/metric.py", line 152, in wrapped
raise exception # pylint: disable=raising-bad-type
File "/usr/local/Cellar/aws-sam-cli/1.21.1/libexec/lib/python3.8/site-packages/samcli/lib/telemetry/metric.py", line 121, in wrapped
return_value = func(*args, **kwargs)
File "/usr/local/Cellar/aws-sam-cli/1.21.1/libexec/lib/python3.8/site-packages/samcli/lib/utils/version_checker.py", line 42, in wrapped
actual_result = func(*args, **kwargs)
File "/usr/local/Cellar/aws-sam-cli/1.21.1/libexec/lib/python3.8/site-packages/samcli/cli/main.py", line 90, in wrapper
return func(*args, **kwargs)
File "/usr/local/Cellar/aws-sam-cli/1.21.1/libexec/lib/python3.8/site-packages/samcli/commands/validate/validate.py", line 35, in cli
do_cli(ctx, template_file) # pragma: no cover
File "/usr/local/Cellar/aws-sam-cli/1.21.1/libexec/lib/python3.8/site-packages/samcli/commands/validate/validate.py", line 55, in do_cli
validator.is_valid()
File "/usr/local/Cellar/aws-sam-cli/1.21.1/libexec/lib/python3.8/site-packages/samcli/commands/validate/lib/sam_template_validator.py", line 58, in is_valid
self._replace_local_codeuri()
File "/usr/local/Cellar/aws-sam-cli/1.21.1/libexec/lib/python3.8/site-packages/samcli/commands/validate/lib/sam_template_validator.py", line 76, in _replace_local_codeuri
all_resources = self.sam_template.get("Resources", {})
AttributeError: 'str' object has no attribute 'get'
Frank
Mike McCall
03/26/2021, 7:12 PMnpx sst cdk synth ReplaceMeWithStackName --no-staging -e > template.yaml
2. Delete the sst output at the top of the template ie. Preparing your SST app…
3. Find the lambda function you want to invoke
4. Call it locally sam local invoke ApiLambdaGetNotesFF22D02F --no-event
Frank
Mike McCall
03/26/2021, 7:14 PMMike McCall
03/26/2021, 7:16 PMFrank
Frank
Nicola Brisotto
03/26/2021, 7:27 PMaws lambda invoke --function-name <FUNCTION_ARN> --payload "$(cat mocks/topic-create-event.json)" response.json
could be useful in development but getting the list of ARN is tedious
• To get the list of ARN I’ve tried this and it works aws cloudformation list-stack-resources --region us-east-1 --stack-name dev-course-manager-sst-api | jq '.StackResourceSummaries | .[] | select(.ResourceType=="AWS::Lambda::Function") | [.LogicalResourceId, .PhysicalResourceId]'
For CI I still have to think about it, do you have any suggestion?Mike McCall
03/26/2021, 7:47 PMFrank
sst start
, and u can invoke with them later.Frank
Nicola Brisotto
03/27/2021, 9:51 AMimport * as cdk from "@aws-cdk/core";
import * as sst from "@serverless-stack/resources";
import * as apig from "@aws-cdk/aws-apigatewayv2";
export default class MyStack extends sst.Stack {
constructor(scope, id, props) {
super(scope, id, props);
const { tableName } = props;
// Create the HTTP API
const api = new sst.Api(this, "Api", {
defaultFunctionProps: {
environment: { appTable: tableName },
},
cors: {
allowMethods: [apig.HttpMethod.GET],
},
routes: {
"POST /topics": "crud/create.main",
},
});
// Show API endpoint in output
new cdk.CfnOutput(this, "ApiEndpoint", {
value: api.httpApi.apiEndpoint,
});
}
}
lib/index.js
import S3Stack from "./S3Stack";
import CognitoStack from "./CognitoStack";
import DynamoDBStack from "./DynamoDBStack";
import ApiStack from "./ApiStack";
// Add stacks
export default function main(app) {
const dynamo = new DynamoDBStack(app, "dynamodb");
const s3 = new S3Stack(app, "s3");
new CognitoStack(app, "cognito", { bucketArn: s3.bucket.bucketArn });
new ApiStack(app, "api", {tableName: dynamo.appTable.tableName});
}
Frank
new cdk.CfnOutput(this, "POST_topics_LambdaArn", {
value: api.getFunction("POST /topics").functionArn,
});
It will print out the Lambda function’s ARN after the deployFrank
this.addOutputs({
ApiEndpoint: api.httpApi.apiEndpoint,
POST_topics_LambdaArn: api.getFunction("POST /topics").functionArn,
});
Nicola Brisotto
03/28/2021, 8:24 PMFrank