Building a weather monitor with AWS SAM and Terraform — part 2

Richard Chou
3 min readNov 26, 2023

--

Source code can be found here

Part 1 Part 3

This article is the “state machine” part of the weather monitor. It is comprised of Step Functions and DynamoDB. Step Functions subscribes the weather data event from Event Bridge, and then insert the data into DynamoDB.

Step Functions

This part says Step Functions subscribes the demo.event (I haven't got time to update this to a more appropriate name, but it's the weather event) from the default Event Bridge.

template.yml

How was the Step Functions definition (workflow.asl.json) generated? Well I followed AWS's suggestion to built my first version of flow on Workflow Studio. Here's what it looks like in Workflow Studio:

Once I was happy with the flow, I exported the flow to json, and put it in my project folder (statemachine/workflow.asl.json).

The rule is simple: if temperature, humidity, time and city exist, insert the weather data into DynamoDB. Otherwise go to end state (do nothing).

statemachine/workflow.asl.json

Testing Step Functions

There are some resources to test Step Functions. I encourage you to take a look.

  1. Data flow simulator

2. Local development tools

Step Functions Transformation

Step Functions has various input and output variables. I found this blog and its sample application are really helpful explaining them.

DynamoDB

Weather data is being inserted into DynamoDB by Step Functions. Here’s the definition of DynamoDB table:

template.yml

Hash Key is “city” and Range Key is “datetime” (in unix time) because I want to query the DynamoDB by city, and sort the temperature data by time.

Here’s what data looks like in DynamoDB:

Developers and entrepreneurs:

Do you have a business idea, and want to run it on AWS, but don’t know how?

This ebook will show you how to build a full web-stack from scratch, with AWS Copilot.

Focus your time on the business side of things instead of connecting AWS resources.

(Source code included)

--

--

Richard Chou

I write about Ruby, Rails, AWS and JavaScript. Occasionally other things.