Building a Go Application Inside of AWS CloudShell
2022 September 29

I’ve been learning a little bit of golang as of late, and I wanted to run it on AWS Lambda. I had a couple errors come up about my libc, and learned that I need to build my applications on Amazon Linux, so they can run on lambda. I didn’t want to set up a CodeBuild just to play with a few toy example programs or setup a couple docker containers. In fact we don’t even need to since AWS provides CloudShell, a complete shell environment available to every user of an AWS account.

If execution is attempted with a go binary not built on Amazon Linux, some of the following errors might look familar. Especially the missing libc versions and the higher than expected billed duration.

2022-09-29T10:37:08.741-04:00	/var/task/main: /lib64/libc.so.6: version `GLIBC_2.32' not found (required by /var/task/main)
2022-09-29T10:37:08.741-04:00	/var/task/main: /lib64/libc.so.6: version `GLIBC_2.34' not found (required by /var/task/main)
2022-09-29T10:37:08.741-04:00	2022/09/29 14:37:08 exit status 1
2022-09-29T10:37:11.513-04:00	START RequestId: 084cc144-bbd8-4672-b091-dbd292a09875 Version: $LATEST
2022-09-29T10:37:11.553-04:00	END RequestId: 084cc144-bbd8-4672-b091-dbd292a09875
REPORT RequestId: 084cc144-bbd8-4672-b091-dbd292a09875	Duration: 232.20 ms	Billed Duration: 233 ms	Memory Size: 128 MB	Max Memory Used: 5 MB	
XRAY TraceId: 1-6335ad97-71e9aec602af57f33d507890	SegmentId: 30b0f3c90f914973	Sampled: true	
REPORT RequestId: 084cc144-bbd8-4672-b091-dbd292a09875 Duration: 232.20 ms Billed Duration: 233 ms Memory Size: 128 MB Max Memory Used: 5 MB XRAY TraceId: 1-6335ad97-71e9aec602af57f33d507890 SegmentId: 30b0f3c90f914973 Sampled: true

There’s a couple problems that must be resolved first though. The home directory in CloudShell is really small with only 1GB available, and golang requires downloading a lot of large packages. Also any package that are installed into the image are blown away afterwards. However the system disk is much bigger at a generous 30GB. This can be resolved by creating a new directory outside of home, and setting the GOPATH to this directory.

So to setup golang the following commands are ran. I recommend putting them into a file to source each time the cloudshell environment is sourced. It’s be sourced not run, as certain environment variables need to be set.

sudo yum install -y golang
sudo mkdir /go
sudo chown $(whoami):$(whoami) /go
export GOPATH=/go

If you have any go binaries you want to include in the PATH, then it should also be updated, but I do not have any such utilities. After running the go build should work perfectly with your project. We’ll use the example lambda function from aws-lambda-go, and my pared down configuration located here.

It should be pretty self explainatory, but you might run into issues with aws cli v2, as mentioned in this issue.

I hope you’ve found this post informative and useful, and that’s it for now.

*****
Written by Henry J Schmale on 2022 September 29
Hit Counter

Remember you can also subscribe using RSS at the top of the page!

Share this on → Twitter LinkedIn Reddit