Run AWS Sam for Golang on Mac M1
on Cloud, Lambda, Golang, Go, Aws, Sam, Macbook, M1, Arm, Apple, Pro
Amazon lambda is a widely used serverless platform, it supports a variety of programming languages but only one of them interests me which is golang. I write a basic tool for serverless run and before deploying this golang application to the lambda, I want to test this in my local.
Amazon has a testing tool which is called sam to test your cloud application on your local. It requires docker to start the test server on your local. When you follow the Amazon guidelines for your lambda deployment it seems to work at first glance.
Sam will start the test container and start to listen :3000 for the request. When you invoke your lambda function, the test server starts to execute your binary to serve lambda service. After a few invokes and tests, you recognized that your software will crash at different times and different steps.
When you look the terminal you will see qemu: uncaught target signal 11 (Segmentation fault) - core dumped
error.
This is happened due to the Macbook M1 cpu architecture.
Solution
Currently aws lambda supports ARM at the cloud deployments but it is not supported on aws sam. So we can not test our software with aws sam natively. There are few containers or tweaks to run but for security purposes, I do not follow custom containers or test softwares. Sam container inside the docker, it breaks at different times, to eliminate this problem we will use emulation to run our software.
Run x64 Linux on your mac
To run our software on mac, we will use linux distro as a host for lambda function. To do this, please follow this article and then continue the below steps.
Install Docker on Alpine
Access your emulator via ssh and enable community repos at apk.
apk add nano
nano /etc/apk/repositories
#uncomment vx.xx/community
#save and exist
apk update
apk add docker
service docker start
rc-update add docker # to enable at boot
Install Sam on Alpine
apk -v --no-cache --update add musl-dev gcc python3 python3-dev
python3 -m ensurepip --upgrade
pip3 install --upgrade pip
pip3 install --upgrade awscli aws-sam-cli
apk del python3-dev gcc musl-dev
Copy projects to emulator
You can use scp to copy files to your host but scp does not check if the remote file is the same as local. To speed up the copy process you can use rsync for copy.
rsync -avzhp --delete -e "ssh -p 10022" components/ root@localhost:/root/sam
Port forwarding
We start a lambda server at alpine but we cannot test on our mac.
To forward emulator ports to local we can use ssh port forwarding.
Open /etc/ssh/sshd_config on alpine and than change AllowTcpForwarding
to yes
nano /etc/ssh/sshd_config
service sshd restart
exit #after server restart reconnect your ssh server
ssh root@127.1 -p 10022 -L "127.0.0.1:3000:127.0.0.1:3000"
I hope this blog will helpfull. Stay safe, see you next time.