Name: unique-port
Owner: Clever
Description: Lambda function for allocating unique ports via cloudformation
Created: 2017-01-11 20:25:38.0
Updated: 2017-01-11 20:54:26.0
Pushed: 2018-02-20 21:26:29.0
Homepage: null
Size: 15
Language: Go
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Lambda-backed custom CloudFormation resource that produces a unique port.
This repo contains:
A JS wrapper (lambada.js) which handles lambda's api and passes event onto a compile go binary
Go code to manage a Port free list
The port free list is managed with two DynamoDB tables:
The first table is used as a distributed lock, since this lambda function will often be called in parallel
The second table manages a free list of ports in the range 10000 - 50000 as a bitset. A set bit indicates that that port is available, and an unset bit means that the port has already been allocated for someone else.
You can build the code with
ke install_deps
ke
This will create the zip file (uniqueport.zip) ready to be uploaded to lambda. Navigate to your existing lambda function in the aws console and use the upload zip function to upload the newly built zip file.
If you don't already have this lambda function setup, the instructions below will set up the lambda function and all the necessary dynamodb tables.
Build the code for the lambda function and upload it to S3:
rt PUBLIC_AWS_BUCKET=clever-public-us-west-2
release
This will upload a file called uniqueport.zip
into the bucket you specify.
Launch the lambda function into a region that has lambda support, e.g. us-west-2:
--region us-west-2 cloudformation create-stack \
ack-name custom-cf-resource-uniqueport \
mplate-body file://`pwd`/custom-cf-resource-uniqueport.json \
pabilities CAPABILITY_IAM \
rameters \
meterKey=S3Bucket,ParameterValue=$PUBLIC_AWS_BUCKET \
meterKey=S3Key,ParameterValue=uniqueport.zip
Now you can use the resource in cloudformation configs. The following example just asks for a few ports (and does it in a different region than the lamda fn):
l of the following are outputs of step 2
rt LAMBDA_ARN=x
rt DYNAMO_REGION=x
rt DYNAMO_LOCK_TABLE=x
rt DYNAMO_PORTS_TABLE=x
aws –region us-west-1 cloudformation create-stack \
–stack-name uniqueport-example \
–template-body file://pwd
/example.json \
–capabilities CAPABILITY_IAM \
–parameters \
ParameterKey=Key,ParameterValue=example-key \
ParameterKey=LambdaArn,ParameterValue=$LAMBDA_ARN \
ParameterKey=DynamoRegion,ParameterValue=$DYNAMO_REGION \
ParameterKey=LockTable,ParameterValue=$DYNAMO_LOCK_TABLE \
ParameterKey=PortsTable,ParameterValue=$DYNAMO_PORTS_TABLE
```