1
1
# Lambda continuous deployment with GitHub actions
2
- This tutorial builds a continuous deployment (CD) pipeline between GitHub and AWS Lambda using GitHub actions.
2
+ This tutorial builds a continuous deployment (CD) pipeline between GitHub and
3
+ AWS Lambda using GitHub actions.
3
4
4
- Packaging and deploying your function and its dependencies with AWS Lambda can sometimes be a tedious job.
5
- Especially if you also want to use a source code management platform like GitHub to develop your code before pushing
6
- it to AWS Lambda.
5
+ Packaging and deploying your function and its dependencies with AWS Lambda can
6
+ sometimes be a tedious job. Especially if you also want to use a source code
7
+ management platform like GitHub to develop your code before pushing it to AWS
8
+ Lambda.
7
9
8
- You can use GitHub actions to set up automatic deployment for AWS Lambda from a Github repository.
9
- You need to push a commit to the main or master branch of your repository, then let GitHub actions create the deployment
10
- package, and deploy your code to AWS Lambda.
10
+ You can use GitHub actions to set up automatic deployment for AWS Lambda from a
11
+ Github repository. You need to push a commit to the ` main ` or ` master ` branch of
12
+ your repository, then let GitHub actions create the deployment package, and
13
+ deploy your code to AWS Lambda.
11
14
12
15
## Prerequisites
13
16
* Git ([ installation options here] ( https://git-scm.com/book/en/v2/Getting-Started-Installing-Git ) )
@@ -44,22 +47,22 @@ Now you can create a new GitHub repository which contains the function code.
44
47
import psycopg2
45
48
import psycopg2.extras
46
49
import os
47
-
50
+
48
51
def lambda_handler(event, context):
49
52
db_name = os.environ[' DB_NAME' ]
50
53
db_user = os.environ[' DB_USER' ]
51
54
db_host = os.environ[' DB_HOST' ]
52
55
db_port = os.environ[' DB_PORT' ]
53
56
db_pass = os.environ[' DB_PASS' ]
54
-
57
+
55
58
conn = psycopg2.connect(user=db_user, database=db_name, host=db_host,
56
59
password=db_pass, port=db_port)
57
-
60
+
58
61
sql = "SELECT * FROM stocks_intraday ORDER BY time DESC LIMIT 10"
59
62
cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
60
63
cursor.execute(sql)
61
64
result = cursor.fetchall()
62
-
65
+
63
66
return {
64
67
' statusCode' : 200,
65
68
' body' : json.dumps(list_of_dicts, default=str),
@@ -73,21 +76,21 @@ Now you can create a new GitHub repository which contains the function code.
73
76
git init
74
77
git add function.py
75
78
git commit -m "Initial commit: add Lambda function"
76
- git branch -M master
79
+ git branch -M main
77
80
git remote add origin <YOUR_GITHUB_PROJECT_URL.git>
78
- git push -u origin master
81
+ git push -u origin main
79
82
```
80
83
81
- At this point, you have a GitHub repository with just the Lambda function in it. Now you can connect this repository
84
+ At this point, you have a GitHub repository with just the Lambda function in it. Now you can connect this repository
82
85
to the AWS Lambda function.
83
86
84
87
## Connect GitHub and AWS Lambda
85
88
86
89
Let' s connect the Github repository AWS Lambda using Github actions.
87
90
88
91
# ## Procedure: Adding your AWS credentials to the repository
89
- You need to add your AWS credentials to the repository so it will have permission to connect to Lambda.
90
- You can do this by adding [GitHub secrets](https://docs.github.com/en/actions/reference/encrypted-secrets)
92
+ You need to add your AWS credentials to the repository so it will have permission to connect to Lambda.
93
+ You can do this by adding [GitHub secrets](https://docs.github.com/en/actions/reference/encrypted-secrets)
91
94
using the GitHub CLI.
92
95
93
96
1. Authenticate with GitHub:
@@ -96,26 +99,26 @@ using the GitHub CLI.
96
99
` ` `
97
100
This prompts you to choose which account you want to log into using either your password or GitHub
98
101
authentication token.
99
- 1. Add AWS credentials as GitHub secrets.
100
- By using GitHub secrets, your credentials are encrypted and cannot be seen
101
- publicly. Use the ` gh secret set` command to upload your AWS credentials one by one
102
+ 1. Add AWS credentials as GitHub secrets.
103
+ By using GitHub secrets, your credentials are encrypted and cannot be seen
104
+ publicly. Use the ` gh secret set` command to upload your AWS credentials one by one
102
105
(you' ll be prompted to paste the values for each one):
103
-
106
+
104
107
AWS_ACCESS_KEY_ID:
105
108
```bash
106
109
gh secret set AWS_ACCESS_KEY_ID
107
110
```
108
-
111
+
109
112
AWS_SECRET_ACCESS_KEY:
110
113
```bash
111
114
gh secret set AWS_SECRET_ACCESS_KEY
112
115
```
113
-
116
+
114
117
AWS_REGION:
115
118
```bash
116
119
gh secret set AWS_REGION
117
120
```
118
-
121
+
119
122
1. To make sure your credentials have been uploaded correctly, you can list the available GitHub secrets:
120
123
```bash
121
124
gh secret list
@@ -134,7 +137,7 @@ to auto-deploy to AWS Lambda.
134
137
```bash
135
138
touch .github/workflows/main.yml
136
139
```
137
-
140
+
138
141
1. Add this content to the file:
139
142
```yml
140
143
name: deploy to lambda
@@ -143,9 +146,9 @@ to auto-deploy to AWS Lambda.
143
146
# but only for the main branch
144
147
push:
145
148
branches:
146
- - master
149
+ - main
147
150
jobs:
148
-
151
+
149
152
deploy_source:
150
153
name: deploy lambda from source
151
154
runs-on: ubuntu-latest
@@ -161,11 +164,11 @@ to auto-deploy to AWS Lambda.
161
164
function_name: lambda-cd
162
165
source: function.py
163
166
```
164
- This configuration will make sure to deploy the code to Lambda when there' s a new push to the master branch.
167
+ This configuration will make sure to deploy the code to Lambda when there' s a new push to the main branch.
165
168
166
- As you can also see in the YAML file, the AWS credentials are accessed using the ` ${{ secrets.AWS_ACCESS_KEY_ID } }`
169
+ As you can also see in the YAML file, the AWS credentials are accessed using the ` ${{ secrets.AWS_ACCESS_KEY_ID } }`
167
170
syntax.
168
- Make sure to use the name of the Lambda function (as displayed in the AWS console) for the ` function_name`
171
+ Make sure to use the name of the Lambda function (as displayed in the AWS console) for the ` function_name`
169
172
property in this configuration file. (" lambda-cd" in this example).
170
173
171
174
# ## Procedure: Testing the pipeline
0 commit comments