This article explains how to create & deploy an AWS State Machine with a .NET Core Lambda using AWS SAM.
Prerequisites
- Latest AWS CLI. This guide uses v2.0.19.
- Latest SAM CLI. This guide uses v0.39.0.
- VS Code with AWS Toolkit. This guide uses VS Code v1.45.1 with AWS Toolkit v1.10.0.
- Latest .NET Core SDK. This guide uses v3.1.300.
Step 1 — Create SAM App
In VS Code:

Select .NET Core:

Select Hello World:

Select a folder:

Name the app:

The app is created & the template is displayed:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
Sample SAM Template for sam-app
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 10
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: ./src/HelloWorld/
Handler: HelloWorld::HelloWorld.Function::FunctionHandler
Runtime: dotnetcore2.1
Environment: # More info about Env Vars: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#environment-object
Variables:
PARAM1: VALUE
Events:
HelloWorld:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /hello
Method: get
Outputs:
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
# Find out more about other implicit resources you can reference within SAM
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
HelloWorldApi:
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
HelloWorldFunction:
Description: "Hello World Lambda Function ARN"
Value: !GetAtt HelloWorldFunction.Arn
HelloWorldFunctionIamRole:
Description: "Implicit IAM Role created for Hello World function"
Value: !GetAtt HelloWorldFunctionRole.Arn
Delete the highlighted lines above.
Step 2 — Add State Machine
Add the following between lines 11 & 12 in the template:
StateMachine:
Type: AWS::Serverless::StateMachine
Properties:
Definition:
StartAt: HelloWorldLambdaState
States:
HelloWorldLambdaState:
Type: Task
Resource: !GetAtt HelloWorldFunction.Arn
End: true
Policies:
- LambdaInvokePolicy:
FunctionName: !Ref HelloWorldFunction
Step 3 — Deploy SAM App
In VS Code:

Select template:

Select region:

Provide an S3 bucket:

Name the CloudFormation stack:

Wait for deployment:
Starting SAM Application deployment...
Building SAM Application...
Packaging SAM Application to S3 Bucket: sam-app-code-c6ba893e
Deploying SAM Application to CloudFormation Stack: sam-app-stack
Successfully deployed SAM Application to CloudFormation Stack: sam-app-stack
Step 4 — Test State Machine
Open the state machine in AWS console & start execution:


In a few seconds, the execution completes & output is displayed:
