Skip to content

Instantly share code, notes, and snippets.

@sasha1sum
Last active August 23, 2019 09:40
Show Gist options
  • Save sasha1sum/e200ed6699addb6c1052eb0deb41c0f1 to your computer and use it in GitHub Desktop.
Save sasha1sum/e200ed6699addb6c1052eb0deb41c0f1 to your computer and use it in GitHub Desktop.
Example of running .NET executables with AWS Lambda and Mono

Example of running .NET executables with AWS Lambda and Mono

This is a simple .NET application which does reads from stdin and prints the result backwards. This is compiled with mono and executed with AWS lambda.

Instructions

  • Setup
    • boot a new Amazon Linux EC2 instance (t2.micro is fine)
    • Edit /etc/yum.repos.d/epel.repo
    • set enabled=1 in [epel] section
    • run sudo yum install mono-core mono-extras mono-devel
    • run sudo yum groupinstall "Development Tools"
  • Build
    • git clone https://gist.github.com/e200ed6699addb6c1052eb0deb41c0f1.git reverse_cat
    • cd reverse_cat
    • make
  • Deploy
    • Upload reverse_cat.zip to s3
    • Upload provided cloud template to s3 folder
    • Optional edit make file to point to a bucket your ec2 has access to, run make upload
    • Launch cloudformation template
    • specify the bucket and full key to the uploaded zip file
    • acknowledge that this formation creates an IAM role

Once deployed you can execute any test object with the lambda and it will reverse the strigified test object.

from __future__ import print_function
import json
import os
from subprocess import Popen, PIPE
def run_exec(*args, **envArgs):
env = dict(os.environ)
env.update(envArgs)
return Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE, env=env)
def handler(event, context):
cwd = os.path.abspath( os.getcwd() )
exe = os.path.join(cwd, "reverse_cat")
lib = os.path.join(cwd, "lib")
proc = run_exec(exe, LD_LIBRARY_PATH=lib)
proc.stdin.write(json.dumps(event, indent=2))
proc.stdin.close()
# ran successfully
exitcode = proc.wait()
if exitcode == 0:
msg = ''.join(proc.stdout.readlines())
print(msg)
return msg
else:
raise Exception("exitcode of %d" % exitcode)
S3_BUCKET = YOUR_BUCKET
S3_PREFIX = YOUR_PATH
all: build
upload: reverse_cat.zip reverse_cat_cf_template.json
aws s3 cp reverse_cat.zip s3://$(S3_BUCKET)/$(S3_PREFIX)/reverse_cat.zip
aws s3 cp reverse_cat.zip s3://$(S3_BUCKET)/$(S3_PREFIX)/reverse_cat_cf_template.json
build: reverse_cat.zip
reverse_cat.zip: lib reverse_cat lambda_function.py
zip -r reverse_cat.zip lib/ reverse_cat lambda_function.py
reverse_cat: ReverseCat.exe
mkbundle -o reverse_cat ReverseCat.exe --deps
ReverseCat.exe: ReverseCat.cs
mcs ReverseCat.cs
lib: reverse_cat
mkdir lib
ldd reverse_cat | awk '/=> \/usr/ { print ("cp " $$3 " lib"); }' | /bin/sh
clean:
rm -f reverse_cat
rm -rf lib
rm -f ReverseCat.exe
rm -f reverse_cat.zip
{
"Description": "Reverse Cat Lambda",
"Parameters": {
"S3Bucket": {
"Type": "String"
},
"S3Key": {
"Type": "String"
}
},
"Resources": {
"Lambda": {
"Properties": {
"Code": {
"S3Bucket": {
"Ref": "S3Bucket"
},
"S3Key": {
"Ref": "S3Key"
}
},
"Description": "Lambda which executes mono executable `reverse_cat`",
"Handler": "lambda_function.handler",
"MemorySize": 128,
"Role": {
"Fn::GetAtt": [
"LambdaRole",
"Arn"
]
},
"Runtime": "python2.7",
"Timeout": 30
},
"Type": "AWS::Lambda::Function"
},
"LambdaRole": {
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
}
}
],
"Version": "2012-10-17"
},
"Policies": [
{
"PolicyDocument": {
"Statement": [
{
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Effect": "Allow",
"Resource": "arn:aws:logs:*:*:*"
}
],
"Version": "2012-10-17"
},
"PolicyName": "LambdaRolePolicy"
}
]
},
"Type": "AWS::IAM::Role"
}
}
}
using System;
using System.Collections.Generic;
public class ReverseCat
{
static public void Main ()
{
List<string> input = new List<string>();
string line;
while ((line = Console.ReadLine()) != null) {
input.Add(line);
}
char[] linesArray = String.Join("\n", input.ToArray()).ToCharArray();
Array.Reverse(linesArray);
Console.WriteLine (new string(linesArray));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment