Enable Logging in State Machine Using CloudFormation
Tags: awscloudformation, awsstepfunction, lambda, cloudwatch
Published: 2023-02-18 11:02:11

“ I have checked the documents of AWS to enable logging in state machine using cloudformation. It's easy to enable the log feature using cloudformation. The Pricing is based on services usage.”

AWS CloudFormation is a service that helps you model and set up your AWS resources so that you can speed less time managing those resources and more time focusing on your applications that run in AWS. You create a template that describes all the AWS resources that you want (like Amazon EC2 instances or Amazon RDS DB instances), and CloudFormation takes care of provisioning and configuring those resources for you. 

AWS Step Functions is a serverless orchestration service that lets you combine AWS Lambda functions and other AWS services to build business-critical applications. Through Step Functions graphical console, you see your application’s workflow as a series of event-driven steps. Step Functions is based on state machines and tasks. A state machine is a workflow. A task is a state in a workflow that represents a single unit of work that another AWS service performs. Each step in a workflow is a state.

In this post, you will experience to enable logging in state machine using cloudformation. Here I have created a step function (a state machine) from cloudformation with a lambda function invoke. Also added cf parameter for logging feature in template. 

#Prerequisites
You’ll need an AWS Lambda for this post. [Getting started with aws Lambda](https://aws.amazon.com/lambda/getting-started/) provides instructions on how to create a lambda function. For this blog, I assume that I have a lambda function created.

#Architecture Overview
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3r3qu1xb41c3onug5aqf.png)
The architecture diagram shows the overall deployment architecture with data flow, s3, cloudformation, lambda, cloudwatch, step function.

#Solution overview
The blog post consists of the following phases:
1. Create of S3 Bucket and Upload of Step Function Json File
2. Create of Cloudformation Stack for Step Function with Logging Feature Enabled and Lambda Function Invoke from S3 URI

I have a lambda function as below →
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2yd0byuqhq5xukzn8uy7.png)
##Phase 1: Create of S3 Bucket and Upload of Step Function Json File
1. Open the s3 console and create a bucket with default settings. Add a json file in it which contains lambda function arn to invoke it in step function.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zf4tnqjzyhigfksm1wt9.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jkii8a1dczjyemlozu30.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8b0jwh8wfger19h4v4g1.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1h4mejqyz9nwdmxny058.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bgqnod95h3kxwsxc7pyv.png)

##Phase 2: Create of Cloudformation Stack for Step Function with Logging Feature Enabled and Lambda Function Invoke from S3 URI
1. Open the cloudformation console and create a stack for step function with lambda function invoke.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bc096nqubxcoamh5gvne.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ewa6f82k9e05rmkjo4bj.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6af17ztrg22hw0jz3he7.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cceubot5cp6d3mi91rb0.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zg6kpqu49zy2zijrm4ua.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hsxclknacu0hcxrszk4z.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2mf1w4nlloivl55qd2ak.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d030fyu1b6lbzgxkidlh.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/75c3rquwn8uscj3aa81v.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7qk7zjtv4pjssfpt8oqo.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ta70pq26sh2tevssmrrc.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xhlcebzxrg3a7lbdutsz.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z5v2nkffu7kg1vlg06oe.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eb0zznheq1aa6y99lpag.png)

#Clean-up
Delete CloudFormation Stack, S3 Bucket and Lambda Function.

#Pricing
I review the pricing and estimated cost of this example. 

Cost of CloudWatch = $0.0

Cost of Lambda = $0.0

Cost of Simple Storage Service = $1.0

Cost of Step Functions = $0 for first 4,000 state transitions = $0.0

Total Cost = $(0.0 + 0.0 + 1.0 + 0.0) = $1.0

#Summary
In this post, I showed “how to enable logging in state machine using cloudformation”.

For more details on AWS CloudFormation, Checkout Get started AWS CloudFormation, open the [AWS CloudFormation console](https://us-east-1.console.aws.amazon.com/cloudformation/home?region=us-east-1#/). To learn more, read the [AWS CloudFormation documentation](https://docs.aws.amazon.com/cloudformation/?id=docs_gateway).

For more details on AWS Step Functions, Checkout Get started AWS Step Functions, open the [AWS Step Functions console](https://us-east-1.console.aws.amazon.com/states/home?region=us-east-1#/homepage). To learn more, read the [AWS Step Functions documentation](https://docs.aws.amazon.com/step-functions/?id=docs_gateway).

Thanks for reading!

Connect with me: [Linkedin](https://www.linkedin.com/in/gargee-bhatnagar-6b7223114)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38sdi4nsp89fnui8h7dk.png)