@@ -4,13 +4,17 @@ GitLab allows users to easily deploy AWS Lambda functions and create rich server
...
@@ -4,13 +4,17 @@ GitLab allows users to easily deploy AWS Lambda functions and create rich server
GitLab supports deployment of functions to AWS Lambda using a combination of:
GitLab supports deployment of functions to AWS Lambda using a combination of:
-[Serverless Framework with AWS](https://serverless.com/framework/docs/providers/aws/)
-[Serverless Framework with AWS](#serverless-framework)
-[AWS' Serverless Application Model (SAM)](#aws-serverless-application-model)
- GitLab CI/CD
- GitLab CI/CD
## Serverless Framework
The [Serverless Framework can deploy to AWS](https://serverless.com/framework/docs/providers/aws/).
We have prepared an example with a step-by-step guide to create a simple function and deploy it on AWS.
We have prepared an example with a step-by-step guide to create a simple function and deploy it on AWS.
Additionally, in the [How To section](#how-to), you can read about different use cases,
Additionally, in the [How To section](#how-to), you can read about different use cases like:
like:
- Running a function locally.
- Running a function locally.
- Working with secrets.
- Working with secrets.
...
@@ -18,27 +22,27 @@ like:
...
@@ -18,27 +22,27 @@ like:
Alternatively, you can quickly [create a new project with a template](../../../../gitlab-basics/create-project.md#project-templates). The [`Serverless Framework/JS` template](https://gitlab.com/gitlab-org/project-templates/serverless-framework/) already includes all parts described below.
Alternatively, you can quickly [create a new project with a template](../../../../gitlab-basics/create-project.md#project-templates). The [`Serverless Framework/JS` template](https://gitlab.com/gitlab-org/project-templates/serverless-framework/) already includes all parts described below.
## Example
### Example
In the following example, you will:
In the following example, you will:
1. Create a basic AWS Lambda Node.js function.
1. Create a basic AWS Lambda Node.js function.
1. Link the function to an API Gateway `GET` endpoint.
1. Link the function to an API Gateway `GET` endpoint.
### Steps
#### Steps
The example consists of the following steps:
The example consists of the following steps:
1. Creating a Lambda handler function
1. Creating a Lambda handler function.
1. Creating a `serverless.yml` file
1. Creating a `serverless.yml` file.
1. Crafting the `.gitlab-ci.yml` file
1. Crafting the `.gitlab-ci.yml` file.
1. Setting up your AWS credentials with your GitLab account
1. Setting up your AWS credentials with your GitLab account.
1. Deploying your function
1. Deploying your function.
1. Testing the deployed function
1. Testing the deployed function.
Lets take it step by step.
Lets take it step by step.
### Creating a Lambda handler function
#### Creating a Lambda handler function
Your Lambda function will be the primary handler of requests. In this case we will create a very simple Node.js `hello` function:
Your Lambda function will be the primary handler of requests. In this case we will create a very simple Node.js `hello` function:
...
@@ -67,7 +71,7 @@ In our case, `module.exports.hello` defines the `hello` handler that will be ref
...
@@ -67,7 +71,7 @@ In our case, `module.exports.hello` defines the `hello` handler that will be ref
You can learn more about the AWS Lambda Node.js function handler and all its various options here: <https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html>
You can learn more about the AWS Lambda Node.js function handler and all its various options here: <https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html>
### Creating a `serverless.yml` file
#### Creating a `serverless.yml` file
In the root of your project, create a `serverless.yml` file that will contain configuration specifics for the Serverless Framework.
In the root of your project, create a `serverless.yml` file that will contain configuration specifics for the Serverless Framework.
...
@@ -94,7 +98,7 @@ The `events` declaration will create a AWS API Gateway `GET` endpoint to receive
...
@@ -94,7 +98,7 @@ The `events` declaration will create a AWS API Gateway `GET` endpoint to receive
You can read more about the available properties and additional configuration possibilities of the Serverless Framework here: <https://serverless.com/framework/docs/providers/aws/guide/serverless.yml/>
You can read more about the available properties and additional configuration possibilities of the Serverless Framework here: <https://serverless.com/framework/docs/providers/aws/guide/serverless.yml/>
### Crafting the `.gitlab-ci.yml` file
#### Crafting the `.gitlab-ci.yml` file
In a `.gitlab-ci.yml` file in the root of your project, place the following code:
In a `.gitlab-ci.yml` file in the root of your project, place the following code:
...
@@ -122,7 +126,7 @@ This example code does the following:
...
@@ -122,7 +126,7 @@ This example code does the following:
- Deploys the serverless function to your AWS account using the AWS credentials
- Deploys the serverless function to your AWS account using the AWS credentials
defined above.
defined above.
### Setting up your AWS credentials with your GitLab account
#### Setting up your AWS credentials with your GitLab account
In order to interact with your AWS account, the GitLab CI/CD pipelines require both `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` to be defined in your GitLab settings under **Settings > CI/CD > Variables**.
In order to interact with your AWS account, the GitLab CI/CD pipelines require both `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` to be defined in your GitLab settings under **Settings > CI/CD > Variables**.
For more information please see: <https://docs.gitlab.com/ee/ci/variables/README.html#via-the-ui>
For more information please see: <https://docs.gitlab.com/ee/ci/variables/README.html#via-the-ui>
...
@@ -130,7 +134,7 @@ For more information please see: <https://docs.gitlab.com/ee/ci/variables/README
...
@@ -130,7 +134,7 @@ For more information please see: <https://docs.gitlab.com/ee/ci/variables/README
NOTE: **Note:**
NOTE: **Note:**
The AWS credentials you provide must include IAM policies that provision correct access control to AWS Lambda, API Gateway, CloudFormation, and IAM resources.
The AWS credentials you provide must include IAM policies that provision correct access control to AWS Lambda, API Gateway, CloudFormation, and IAM resources.
### Deploying your function
#### Deploying your function
`git push` the changes to your GitLab repository and the GitLab build pipeline will automatically deploy your function.
`git push` the changes to your GitLab repository and the GitLab build pipeline will automatically deploy your function.
...
@@ -142,7 +146,7 @@ endpoints:
...
@@ -142,7 +146,7 @@ endpoints:
GET - https://u768nzby1j.execute-api.us-east-1.amazonaws.com/production/hello
GET - https://u768nzby1j.execute-api.us-east-1.amazonaws.com/production/hello
```
```
### Manually testing your function
#### Manually testing your function
Running the following `curl` command should trigger your function.
Running the following `curl` command should trigger your function.
...
@@ -165,7 +169,7 @@ Hooray! You now have a AWS Lambda function deployed via GitLab CI.
...
@@ -165,7 +169,7 @@ Hooray! You now have a AWS Lambda function deployed via GitLab CI.
Nice work!
Nice work!
## How To
### How To
In this section, we show you how to build on the basic example to:
In this section, we show you how to build on the basic example to:
...
@@ -173,7 +177,7 @@ In this section, we show you how to build on the basic example to:
...
@@ -173,7 +177,7 @@ In this section, we show you how to build on the basic example to:
- Set up secret variables.
- Set up secret variables.
- Set up CORS.
- Set up CORS.
### Running function locally
#### Running function locally
The `serverless-offline` plugin allows to run your code locally. To run your code locally:
The `serverless-offline` plugin allows to run your code locally. To run your code locally:
...
@@ -204,7 +208,7 @@ It should output:
...
@@ -204,7 +208,7 @@ It should output:
}
}
```
```
### Secret variables
#### Secret variables
Secrets are injected into your functions using environment variables.
Secrets are injected into your functions using environment variables.
...
@@ -225,7 +229,7 @@ NOTE: **Note:**
...
@@ -225,7 +229,7 @@ NOTE: **Note:**
Anyone with access to the AWS environment may be able to see the values of those
Anyone with access to the AWS environment may be able to see the values of those
variables persisted in the lambda definition.
variables persisted in the lambda definition.
### Setting up CORS
#### Setting up CORS
If you want to set up a web page that makes calls to your function, like we have done in the [template](https://gitlab.com/gitlab-org/project-templates/serverless-framework/), you need to deal with the Cross-Origin Resource Sharing (CORS).
If you want to set up a web page that makes calls to your function, like we have done in the [template](https://gitlab.com/gitlab-org/project-templates/serverless-framework/), you need to deal with the Cross-Origin Resource Sharing (CORS).