|
| 1 | +--- |
| 2 | +title: Serverless Framework |
| 3 | +description: Use the Serverless Framework with LocalStack. |
| 4 | +template: doc |
| 5 | +sidebar: |
| 6 | + order: 1 |
| 7 | +--- |
| 8 | + |
| 9 | +## Overview |
| 10 | + |
| 11 | +This guide explains how to integrate LocalStack with the [Serverless Framework](https://www.serverless.com/). |
| 12 | +Although it probably requires a few code changes, integrating LocalStack with the Serverless Framework is fairly straightforward. |
| 13 | + |
| 14 | +In particular, the setup consists of the following two steps. |
| 15 | + |
| 16 | +1. Installing and configuring the [Serverless-LocalStack plugin](https://github.com/localstack/serverless-localstack). |
| 17 | +2. Adjusting AWS endpoints in Lambda functions. |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | + |
| 21 | +This guide assumes that you have the following tools installed. |
| 22 | + |
| 23 | +* LocalStack ([Install](https://docs.localstack.cloud/get-started/#installation)) |
| 24 | +* Serverless ([Install](https://www.serverless.com/framework/docs/getting-started/)) |
| 25 | + |
| 26 | +It also assumes that you already have a Serverless app set up consisting of a couple of Lambda functions and a `serverless.yml` file similar to the following. |
| 27 | +An example Serverless app integrated with LocalStack can be found here: <a href="https://github.com/localstack/serverless-python-rest-api-with-dynamodb"><i class="fab fa-github"></i> Simple REST API using the Serverless Framework and LocalStack</a> |
| 28 | + |
| 29 | +```yaml |
| 30 | +service: my-service |
| 31 | + |
| 32 | +frameworkVersion: ">=1.1.0 <=2.50.0" |
| 33 | + |
| 34 | +provider: |
| 35 | + name: aws |
| 36 | + runtime: python3.8 |
| 37 | + environment: |
| 38 | + DYNAMODB_TABLE: ${self:service}-${opt:stage, self:provider.stage} |
| 39 | + iamRoleStatements: |
| 40 | + - Effect: Allow |
| 41 | + Action: |
| 42 | + - dynamodb:Query |
| 43 | + - ... |
| 44 | + Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.DYNAMODB_TABLE}" |
| 45 | + |
| 46 | +functions: |
| 47 | + create: |
| 48 | + handler: todos/create.create |
| 49 | + events: |
| 50 | + - http: |
| 51 | + path: todos |
| 52 | + method: post |
| 53 | + cors: true |
| 54 | + |
| 55 | + ... |
| 56 | + |
| 57 | +resources: |
| 58 | + Resources: |
| 59 | + TodosDynamoDbTable: |
| 60 | + Type: 'AWS::DynamoDB::Table' |
| 61 | + DeletionPolicy: Retain |
| 62 | + Properties: |
| 63 | + ... |
| 64 | + TableName: ${self:provider.environment.DYNAMODB_TABLE} |
| 65 | +``` |
| 66 | +
|
| 67 | +## Install and configure Serverless-LocalStack Plugin |
| 68 | +
|
| 69 | +To install the plugin, execute the following command in the root of your project. |
| 70 | +```bash |
| 71 | +npm install -D serverless-localstack |
| 72 | +``` |
| 73 | + |
| 74 | +Next, set up the plugin by adding the following properties to `serverless.yml`. |
| 75 | + |
| 76 | +```yaml |
| 77 | +... |
| 78 | + |
| 79 | +plugins: |
| 80 | + - serverless-localstack |
| 81 | + |
| 82 | +custom: |
| 83 | + localstack: |
| 84 | + stages: |
| 85 | + - local |
| 86 | +``` |
| 87 | +
|
| 88 | +This sets up Serverless to use the LocalStack plugin but only for the stage "local". |
| 89 | +Next, you need make minor adjustments to your function code in order to make your application work no matter if it is deployed on AWS or LocalStack. |
| 90 | +
|
| 91 | +## Adjust AWS endpoints in Lambda functions |
| 92 | +
|
| 93 | +You are likely using an AWS SDK (such as [Boto3](https://github.com/boto/boto3) for Python) in your Lambda functions to interact with other AWS services such as DynamoDB. |
| 94 | +
|
| 95 | +For example, in Python, your code to set up a connection to DynamoDB may look like this: |
| 96 | +
|
| 97 | +```python |
| 98 | +... |
| 99 | +dynamodb = boto3.resource('dynamodb') |
| 100 | +... |
| 101 | +``` |
| 102 | + |
| 103 | +By default, this call attempts to create a connection via the usual AWS endpoints. |
| 104 | +However, when running services in LocalStack, we need to make sure, our applications creates a connection via the LocalStack endpoint instead. |
| 105 | + |
| 106 | +Usually, all of LocalStack's services are available via a specific port on localhost (e.g. `localhost:4566`). |
| 107 | +However, this endpoint only works when accessing LocalStack from outside its Docker runtime. |
| 108 | + |
| 109 | +Since the Lambda functions execute within the LocalStack Docker container, Lambda functions cannot access other services via the usual localhost endpoint. |
| 110 | + |
| 111 | +Instead, LocalStack provides a special environment variable `AWS_ENDPOINT_URL` which contains the internal endpoint of the LocalStack services from within its runtime environment. |
| 112 | + |
| 113 | +Hence, you need to configure the Lambda functions to use the `AWS_ENDPOINT_URL` endpoint when accessing other AWS services in LocalStack. |
| 114 | + |
| 115 | +In Python, this may look something like. |
| 116 | +The code detects if it is running in LocalStack by checking if the `AWS_ENDPOINT_URL` variable exists and then configures the endpoint URL accordingly. |
| 117 | + |
| 118 | +```python |
| 119 | +... |
| 120 | +if 'AWS_ENDPOINT_URL' in os.environ: |
| 121 | + dynamodb = boto3.resource('dynamodb', endpoint_url=os.environ['AWS_ENDPOINT_URL']) |
| 122 | +else: |
| 123 | + dynamodb = boto3.resource('dynamodb') |
| 124 | +... |
| 125 | +``` |
| 126 | + |
| 127 | +In LocalStack Pro, no code changes are required using our [Transparent Endpoint Injection](/aws/tooling/transparent-endpoint-injection). |
| 128 | + |
| 129 | +## Deploying to LocalStack |
| 130 | + |
| 131 | +You can now deploy your Serverless service to LocalStack. |
| 132 | + |
| 133 | +First, start LocalStack by running |
| 134 | +```bash |
| 135 | +localstack start |
| 136 | +``` |
| 137 | + |
| 138 | +Then deploy the endpoint by running |
| 139 | +```bash |
| 140 | +serverless deploy --stage local |
| 141 | +``` |
| 142 | + |
| 143 | +The expected result should be similar to: |
| 144 | + |
| 145 | +```bash |
| 146 | +Serverless: Packaging service... |
| 147 | +Serverless: Excluding development dependencies... |
| 148 | +Serverless: Creating Stack... |
| 149 | +Serverless: Checking Stack create progress... |
| 150 | +........ |
| 151 | +Serverless: Stack create finished... |
| 152 | +Serverless: Uploading CloudFormation file to S3... |
| 153 | +Serverless: Uploading artifacts... |
| 154 | +Serverless: Uploading service my-service.zip file to S3 (38.3 KB)... |
| 155 | +Serverless: Validating template... |
| 156 | +Serverless: Skipping template validation: Unsupported in Localstack |
| 157 | +Serverless: Updating Stack... |
| 158 | +Serverless: Checking Stack update progress... |
| 159 | +..................................... |
| 160 | +Serverless: Stack update finished... |
| 161 | +Service Information |
| 162 | +service: my-service |
| 163 | +stage: local |
| 164 | +region: us-east-1 |
| 165 | +stack: my-service-local |
| 166 | +resources: 35 |
| 167 | +api keys: |
| 168 | + None |
| 169 | +endpoints: |
| 170 | + http://localhost:4566/restapis/XXXXXXXXXX/local/_user_request_ |
| 171 | +functions: |
| 172 | + ... |
| 173 | +layers: |
| 174 | + None |
| 175 | +``` |
| 176 | + |
| 177 | +Use the displayed endpoint `http://localhost:4566/restapis/XXXXXXXXXX/local/_user_request_/my/custom/endpoint` to make requests to the deployed service. |
| 178 | + |
| 179 | +## Advanced topics |
| 180 | + |
| 181 | +### Local code mounting for lambda functions |
| 182 | + |
| 183 | +serverless-localstack supports a feature for lambda functions that allows local code mounting: |
| 184 | + |
| 185 | +```yaml |
| 186 | +# serverless.yml |
| 187 | + |
| 188 | +custom: |
| 189 | + localstack: |
| 190 | + # ... |
| 191 | + lambda: |
| 192 | + mountCode: True |
| 193 | +``` |
| 194 | +
|
| 195 | +When this flag is set, the lambda code will be mounted into the container running the function directly from your local directory instead of packaging and uploading it. |
| 196 | +
|
| 197 | +## Ran into trouble? |
| 198 | +
|
| 199 | +If you run into any issues or problems while integrating LocalStack with your Serverless app, please [submit an issue](https://github.com/localstack/serverless-localstack/issues). |
0 commit comments