Skip to content

Commit c87438f

Browse files
committed
mysql2, IAM auth
1 parent e4cf666 commit c87438f

File tree

8 files changed

+88
-19
lines changed

8 files changed

+88
-19
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/bin/bash
22
set -eo pipefail
3-
FUNCTION=$(aws cloudformation describe-stack-resource --stack-name rds-mysql --logical-resource-id dbadmin --query 'StackResourceDetail.PhysicalResourceId' --output text)
3+
FUNCTION=$(aws cloudformation describe-stack-resource --stack-name rds-mysql --logical-resource-id function --query 'StackResourceDetail.PhysicalResourceId' --output text)
44
aws lambda invoke --function-name $FUNCTION --payload file://events/db-create-table.json out.json

sample-apps/rds-mysql/6-invoke.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22
set -eo pipefail
3-
FUNCTION=$(aws cloudformation describe-stack-resource --stack-name rds-mysql --logical-resource-id dbadmin --query 'StackResourceDetail.PhysicalResourceId' --output text)
3+
FUNCTION=$(aws cloudformation describe-stack-resource --stack-name rds-mysql --logical-resource-id function --query 'StackResourceDetail.PhysicalResourceId' --output text)
44
55
while true; do
66
aws lambda invoke --function-name $FUNCTION --payload file://events/db-read-table.json out.json

sample-apps/rds-mysql/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ Finally, view the application in the Lambda console.
110110

111111
![Application](/sample-apps/rds-mysql/images/rdsmysql-application.png)
112112

113+
# Use IAM authorization with a database proxy
114+
115+
This application includes a second handler that uses the function's credentials to authenticate ([index-iam.js](/sample-apps/rds-mysql/dbadmin/index-iam.js)). You can use this method to connect to an RDS Proxy without configuring the function with a database password.
116+
117+
For more information, see [Configuring database access](https://docs.aws.amazon.com/lambda/latest/dg/configuration-database.html) in the AWS Lambda Developer Guide.
118+
113119
# Cleanup
114120

115121
To delete the application, run the cleanup script.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const AWSXRay = require('aws-xray-sdk-core')
2+
const captureMySQL = require('aws-xray-sdk-mysql')
3+
const mysql = captureMySQL(require('mysql2'))
4+
const AWS = require('aws-sdk')
5+
const username = process.env.databaseUser
6+
const host = process.env.databaseHost
7+
const database = process.env.databaseName
8+
const region = process.env.AWS_REGION
9+
const sqlport = 3306
10+
11+
const signer = new AWS.RDS.Signer({
12+
region: region,
13+
hostname: host,
14+
port: sqlport,
15+
username: username
16+
})
17+
18+
exports.handler = async (event) => {
19+
let connectionConfig = {
20+
host : host,
21+
user : username,
22+
database : database,
23+
ssl: 'Amazon RDS',
24+
authPlugins: { mysql_clear_password: () => () => signer.getAuthToken() }
25+
}
26+
var connection = mysql.createConnection(connectionConfig)
27+
var query = event.query
28+
var result
29+
connection.connect()
30+
31+
connection.query(query, function (error, results, fields) {
32+
if (error) throw error
33+
console.log("Ran query: " + query)
34+
for (result in results)
35+
console.log(results[result])
36+
})
37+
38+
return new Promise( ( resolve, reject ) => {
39+
connection.end( err => {
40+
if ( err )
41+
return reject( err )
42+
const response = {
43+
statusCode: 200,
44+
body: JSON.stringify(result),
45+
}
46+
resolve(response)
47+
})
48+
})
49+
}

sample-apps/rds-mysql/dbadmin/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var AWSXRay = require('aws-xray-sdk-core')
22
var captureMySQL = require('aws-xray-sdk-mysql')
3-
var mysql = captureMySQL(require('mysql'))
3+
var mysql = captureMySQL(require('mysql2'))
44
const username = process.env.databaseUser
55
const password = process.env.databasePassword
66
const host = process.env.databaseHost

sample-apps/rds-mysql/dbadmin/package.json

Lines changed: 0 additions & 12 deletions
This file was deleted.

sample-apps/rds-mysql/lib/nodejs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"aws-xray-sdk-core": "2.4.0",
77
"aws-xray-sdk-mysql": "2.4.0",
88
"md5": "2.2.1",
9-
"mysql": "2.17.1"
9+
"mysql2": "2.1.0"
1010
},
1111
"scripts": {}
1212
}

sample-apps/rds-mysql/template.yml

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,40 @@ Resources:
4848
CompatibleRuntimes:
4949
- nodejs10.x
5050
- nodejs12.x
51-
dbadmin:
51+
function:
5252
Type: AWS::Serverless::Function
5353
Properties:
5454
CodeUri: dbadmin/.
5555
Description: Run SQL queries.
5656
MemorySize: 128
5757
Timeout: 15
5858
# Function's execution role
59+
Role: !GetAtt role.Arn
60+
role:
61+
Type: AWS::IAM::Role
62+
Properties:
63+
AssumeRolePolicyDocument:
64+
Version: "2012-10-17"
65+
Statement:
66+
-
67+
Effect: Allow
68+
Principal:
69+
Service:
70+
- lambda.amazonaws.com
71+
Action:
72+
- sts:AssumeRole
73+
ManagedPolicyArns:
74+
- arn:aws:iam::aws:policy/AWSXrayWriteOnlyAccess
75+
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
76+
- arn:aws:iam::aws:policy/service-role/AWSLambdaRole
77+
- arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole
5978
Policies:
60-
- AWSLambdaBasicExecutionRole
61-
- AWSLambdaVPCAccessExecutionRole
79+
- PolicyName: rds-iamauth
80+
PolicyDocument:
81+
Version: 2012-10-17
82+
Statement:
83+
- Effect: Allow
84+
Action: 'rds-db:connect'
85+
Resource: '*'
86+
Path: /service-role/
87+

0 commit comments

Comments
 (0)