AWS Lambda - Getting Started with Dollar Cost Averaging
Tags: aws, lambda, serverless, crypto
Published: 2023-05-04 03:04:22

## AWS Lambda - Getting Started with Dollar Cost Averaging

*Investing carries risks. Investors should study the information before making an investment decision.*

The inspiration for this study came from seeing someone in a group trying to post about their investment journey every day. I wanted to try being diligent like them, as shown in the picture below 👇👇👇👇


![Inspiration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ius3u29f8xkaoqw0c2b4.png)

However, I'm too lazy to open the app and make a purchase every day like they do😵 So, I decided to automate🤖 the process using AWS Lambda Function, which allows us to run code serverlessly🚀 and pay only for what we use. I think it's perfect for testing out dollar cost averaging as a beginner investor.


![AWS Lambda Function Price](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z70jf2nif6klognyf7e1.png)

---

### First, let's create an AWS Lambda Function
Log in to the AWS Management Console and search for Lambda:

![Create function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/31hlyd3fouoyx68e837t.png)

1. Click "Create function" and select "Author from scratch"
2. Enter a function name, such as "MyDollarCostAveragingFunction"
3. Choose a runtime for the function, such as Python or Node.js, Java, etc. (Python is used in this example)
4. Create or select a new IAM Role for the Lambda Function
5. Click "Create function" and wait for AWS to create the function...

---

### Next, we will write or upload code for the Lambda Function

![Lambda Function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ekcumdy1c5lhdtipl45g.png)


1.Write code for the function using the initial language you chose. The lambda handler will be provided, and you can add the remaining code.
(Note: You can write the code on your machine and then .zip it to "Upload")
2.Once the code is complete, you can test it by clicking "Test". A dialog will pop up, allowing you to create a simulated event to call your Lambda function (in this case, we don't need to send any parameters).


![Create event](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jjcq36wv43a9zs5o055d.png)


Test the Lambda Function with the event you created, and if everything works correctly, you can deploy it.


![Test event result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9n3tc6uwze14hr2eeb2j.png)


3.In this example, the Lambda Function will call an API to create a market price order at the current time, as the goal is to invest regularly with the same amount of money.

> In dollar cost averaging, the investor decides only two parameters: the fixed amount of money to invest each time period (i.e. the amount that is available to invest) and how often the funds are invested. https://en.wikipedia.org/wiki/Dollar_cost_averaging


![Completed code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d1h1xoqzsnjf1gnl03a7.png)

#### Additional Information
For Python, if you want to add other libraries not provided by Lambda Function, you can do so on your machine as follows:
Open a terminal, create a folder called 'python', and install the requests library for API calls (make sure Python is installed on your machine first)

```
$ mkdir python
$ cd python
$ pip3 install requests -t .
$ zip -r python.zip .
```

![Create Layer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uidaeth80a6u0kv7i4j0.png)

Upload the created .zip file, then "Create" a new Layer. Copy the ARN of the created Layer and add it to your Lambda Function by clicking "Add a layer" and selecting "Specify an ARN". Paste the copied Layer ARN and click "Verify".

![Add Layer to Function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8e145nj8qk83xup4il4l.png)


![Result of layer added](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mykxsx3pn85vavxtfv08.png)

Now you can use the requests library in your Lambda Function.

---

### Lastly, we will set up an Event Trigger to call the function daily

![Lambda function diagram
](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/el1glepanzmj6rbrt47v.png)

![Add event](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xoiuxd0ep1sddtcnlj8j.png)

1. In the Lambda Function page, click "Add trigger"
2. Select "EventBridge (CloudWatch Events)" from the list
3. Create a new Event Rule by clicking "Create a new rule"
4. Enter a name for the Rule, such as "DailyTrigger"
5. Set the Schedule expression to call the function daily by entering `cron(0 12 * * ? *)`, which means every day at 12:00 (UTC time)
6. Click "Add" to add the Trigger to your Lambda function
7. Click "Save" to save the new Trigger settings


![Lambda function result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lkyksna9aqmkg786gz8j.png)

Once you've followed the steps above, your Lambda function will be called daily as per the schedule you've set.


For API specifications or how to make API calls, you can further explore the following resources:[Bitkub](https://github.com/bitkub/bitkub-official-api-docs) , [Binance](https://www.binance.com/en/binance-api)