|
| 1 | +# A simple C++ Monte Carlo simulation calculator on AWS Lambda |
| 2 | + |
| 3 | +Calculate the value of a European vanilla call option in C++ using Monte Carlo simulation. |
| 4 | +Deploy the calculator to AWS Lambda. |
| 5 | +Invoke the calculator with a REST call. |
| 6 | +The number of simulations, `num_sims`, can be overriden with a query parameter. |
| 7 | +All other parameters are hard coded. |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +### Prerequisites |
| 12 | +1. aws account |
| 13 | +2. aws cli |
| 14 | +3. CMake (version 3.9 or later) |
| 15 | +4. git |
| 16 | +5. Make |
| 17 | +6. zip |
| 18 | +7. libcurl4-openssl-dev libssl-dev uuid-dev zlib1g-dev libpulse-dev libcurlpp-dev libcrypto++-dev |
| 19 | + |
| 20 | + |
| 21 | +## Build the C++ AWS SDK |
| 22 | +Run the following commands to build the C++ AWS SDK |
| 23 | +```bash |
| 24 | +$ git clone https://github.com/aws/aws-sdk-cpp.git |
| 25 | +$ cd aws-sdk-cpp |
| 26 | +$ mkdir build |
| 27 | +$ cd build |
| 28 | +$ cmake .. -DBUILD_ONLY="core" -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DCUSTOM_MEMORY_MANAGEMENT=OFF -DCMAKE_INSTALL_PREFIX=~/lambda-install |
| 29 | +$ make |
| 30 | +$ make install |
| 31 | +``` |
| 32 | + |
| 33 | +## Build a custom C++ lambda runtime |
| 34 | +Run the following commands to build the C++ lambda custom runtime: |
| 35 | +```bash |
| 36 | +$ git clone https://github.com/press0/aws-lambda-cpp.git |
| 37 | +$ cd aws-lambda-cpp |
| 38 | +$ mkdir build |
| 39 | +$ cd build |
| 40 | +$ cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=~/lambda-install |
| 41 | +$ make && make install |
| 42 | +``` |
| 43 | + |
| 44 | +## Build the C++ lambda function |
| 45 | +Run the following commands to build the C++ lambda function |
| 46 | + |
| 47 | +```bash |
| 48 | +$ cd ../examples/monte-carlo-calculator/ |
| 49 | +$ mkdir build |
| 50 | +$ cd build |
| 51 | +$ cmake .. -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DCMAKE_PREFIX_PATH=~/lambda-install |
| 52 | +$ make aws-lambda-package-demo |
| 53 | +``` |
| 54 | + |
| 55 | +Verify that a file named `demo.zip` was created. |
| 56 | + |
| 57 | +## Create the AWS IAM resources |
| 58 | + |
| 59 | +``` |
| 60 | +$ cat ../trust-policy.json |
| 61 | +{ |
| 62 | + "Version": "2012-10-17", |
| 63 | + "Statement": [ |
| 64 | + { |
| 65 | + "Effect": "Allow", |
| 66 | + "Principal": { |
| 67 | + "Service": ["lambda.amazonaws.com"] |
| 68 | + }, |
| 69 | + "Action": "sts:AssumeRole" |
| 70 | + } |
| 71 | + ] |
| 72 | +} |
| 73 | +
|
| 74 | +``` |
| 75 | + |
| 76 | +Create the IAM role: |
| 77 | +```bash |
| 78 | +$ aws iam create-role --role-name lambda-demo --assume-role-policy-document file://../trust-policy.json |
| 79 | +``` |
| 80 | +Note the role Arn returned from the command: <API-ENDPOINT-ARN> |
| 81 | + |
| 82 | +Attach the following policy to allow Lambda to write logs in CloudWatch: |
| 83 | +```bash |
| 84 | +$ aws iam attach-role-policy --role-name lambda-demo --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole |
| 85 | +``` |
| 86 | + |
| 87 | +## Create the AWS Lambda function |
| 88 | +```bash |
| 89 | +$ aws lambda create-function --function-name demo --role <API-ENDPOINT-ARN> --runtime provided --timeout 15 --memory-size 128 --handler demo --zip-file fileb://demo.zip |
| 90 | + |
| 91 | +# response |
| 92 | +{ |
| 93 | + "FunctionName": "demo", |
| 94 | + "FunctionArn": "arn:aws:lambda:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 95 | + "Runtime": "provided", |
| 96 | + "Role": "arn:aws:iam::167712817792:role/lambda-demo", |
| 97 | + "Handler": "demo", |
| 98 | + "CodeSize": 13765485, |
| 99 | + "Description": "", |
| 100 | + "Timeout": 15, |
| 101 | + "MemorySize": 128, |
| 102 | + "LastModified": "2023-07-10T20:52:40.434+0000", |
| 103 | + "CodeSha256": "mN/9DIPLA4ZpftWVBTHaTdZsO3nXk+AVHjfyNRoznWg=", |
| 104 | + "Version": "$LATEST", |
| 105 | + "TracingConfig": { |
| 106 | + "Mode": "PassThrough" |
| 107 | + }, |
| 108 | + "RevisionId": "683bf451-3fb4-4a5e-bdf1-0d5fa1158c41", |
| 109 | + "State": "Pending", |
| 110 | + "StateReason": "The function is being created.", |
| 111 | + "StateReasonCode": "Creating", |
| 112 | + "PackageType": "Zip" |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +## Update the function, as needed |
| 117 | +```bash |
| 118 | +aws lambda update-function-code --function-name demo --zip-file fileb://demo.zip |
| 119 | + |
| 120 | + |
| 121 | +``` |
| 122 | + |
| 123 | +## Test the function with the aws cli |
| 124 | +```bash |
| 125 | +$ aws lambda invoke --function-name demo --cli-binary-format raw-in-base64-out --payload '{}' out.txt |
| 126 | + |
| 127 | +$ cat out.txt |
| 128 | +{ |
| 129 | + "message":"OK num_sims=100000, Call price=10.423098 " |
| 130 | +} |
| 131 | + |
| 132 | +``` |
| 133 | + |
| 134 | +## Add a Lambda endpoint |
| 135 | +TODO: use the aws cli or the aws console |
| 136 | + |
| 137 | +`<API-ENDPOINT-ARN>` |
| 138 | + |
| 139 | +## Test the function with curl |
| 140 | + |
| 141 | +```bash |
| 142 | +curl 'https://<API-ENDPOINT-ARN>.lambda-url.us-west-2.on.aws?num_sims=1000000' |
| 143 | +# response |
| 144 | +{ |
| 145 | +"message": "OK num_sims=1000000, Call price=10.459100 " |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +## check performance in CloudWatch |
| 150 | + |
| 151 | +one tenth of a second |
| 152 | + |
| 153 | + |
| 154 | +## Test the function at scale |
| 155 | +todo |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | +## Delete the function |
| 161 | +```bash |
| 162 | +aws lambda delete-function --function-name demo |
| 163 | +``` |
| 164 | + |
| 165 | +## Delete the role |
| 166 | +todo: detach policies first |
| 167 | +```bash |
| 168 | +aws iam delete-role --role-name lambda-demo |
| 169 | +``` |
| 170 | + |
| 171 | +## Citations |
| 172 | +1. https://www.quantstart.com/articles/European-vanilla-option-pricing-with-C-via-Monte-Carlo-methods/ |
| 173 | + |
0 commit comments