Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Statically linking Haskell program using stack on Amazon Linux, to use on AWS Lambda

I'm trying to build a statically linked 'hello world' Haskell program on an EC2 instance, with a view to running it on AWS Lambda.

My only modification to the 'simple' stack.yaml is:

ghc-options:
    "*": -static -optc-static -optl-static -optl-pthread

I first got the following errors:

[ec2-user@ip-172-31-0-238 lambdatest]$ stack build
lambdatest-0.1.0.0: configure
Configuring lambdatest-0.1.0.0...
lambdatest-0.1.0.0: build
Preprocessing executable 'lambdatest' for lambdatest-0.1.0.0...
Linking .stack-work/dist/x86_64-linux/Cabal-1.22.4.0/build/lambdatest/lambdatest ...
/usr/bin/ld: cannot find -lgmp
/usr/bin/ld: cannot find -lm
/usr/bin/ld: cannot find -lrt
/usr/bin/ld: cannot find -ldl
/usr/bin/ld: cannot find -lpthread
/usr/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status

The first thing I tried was to install gmp-devel:

[ec2-user@ip-172-31-0-238 lambdatest]$ sudo yum install gmp-devel.x86_64
Loaded plugins: priorities, update-motd, upgrade-helper
Package gmp-devel-6.0.0-11.16.amzn1.x86_64 already installed and latest version
Nothing to do

but it looks like that isn't the issue.

Next I installed glibc-static and gmp-static, and now the error I see is:

[ec2-user@ip-172-31-0-238 lambdatest]$ stack build
lambdatest-0.1.0.0: build
Preprocessing executable 'lambdatest' for lambdatest-0.1.0.0...
Linking .stack-work/dist/x86_64-linux/Cabal-1.22.4.0/build/lambdatest/lambdatest ...
/home/ec2-user/.stack/programs/x86_64-linux/ghc-7.10.2/lib/ghc-7.10.2/rts/libHSrts.a(Linker.o): In function `internal_dlopen':

Could not resolve file /home/ben/ghc-7.10.2/rts/Linker.c

But when I run the identical 'stack build' command a second time, it completes without error.

like image 352
tgdavies Avatar asked Jan 04 '16 23:01

tgdavies


1 Answers

So what worked for me was:

Add the fpcomplete repo:

curl -sSL https://s3.amazonaws.com/download.fpcomplete.com/centos/7/fpco.repo | \
sudo tee /etc/yum.repos.d/fpco.repo

Install stack:

sudo yum -y install stack

Install ghc:

stack setup

Create a new stack project:

stack new lambdatest simple

Modify stack.yaml with the following flags:

ghc-options:
    "*": -static -optc-static -optl-static -optl-pthread

Install static versions of libraries:

sudo yum install glibc-static
sudo yum install gmp-static

Then run stack build twice!

The resulting executable runs OK on my EC2 instance and also runs on AWS Lambda when started by a Node.js launcher function.

like image 75
tgdavies Avatar answered Oct 17 '22 04:10

tgdavies