A service that allows you to set up and operate a relational database in the cloud. It allows us to create a PostgreSQL instance
- Connect to AWS RDS Postgres shell
psql --host instanceendpoint --port 5432 --username username --dbname dbname
- Ask your instructor for the user password
- Create user with encrypted password
- Grant user to jrdevleague
GRANT your_user TO "jrDevleague";
- Create database named
hta-YOUR_NAME
with newly created user - Connect into newly created database
- Create table student with the following columns
- student_id
- name
- grade_level
- Insert data for one student
- Grant table privileges to user
GRANT ALL PRIVILEGES ON TABLE YOUR_TABLE_NAME TO YOUR_USERNAME;
- Create serverless boilerplate/template
- Change service to
aws-rds-demo-YOURNAME
in yourserverless.yml
- Create a
routes
folder - Add
iamRoleStatements
to allows rds in yourserveless.yml
iamRoleStatements:
- Effect: Allow
Action:
- rds:*
Resource: "*"
- Create a
GET
lambda function inroutes
folder
- set up your http method
- set up your http path
- set up your cors
6.npm install pg pg-pool
7. Create config.json file with the following:
{
"table": "YOUR_TABLE_NAME",
"host": "jrdevleague.cb9co1xxtizk.us-west-2.rds.amazonaws.com",
"database": "YOUR_DATABASE_NAME",
"user": "YOUR_USERNAME",
"password": "YOUR_PASSWORD",
"port": 5432
}
- Add config.json to your
.gitignore
- Require pg-pool
const Pool = require('pg-pool');
- Require config file
const config = require('../config.json')
- Use ES6 Object Destructuring to grab keys/properties from
config.json
const { table, host, database, user, password, port } = config
- Create new pool object
const pool = new Pool({
host,
database,
user,
password,
port,
idleTimeoutMillis: 1000
});
- Create a
SELECT
query and save it into a variable
const getAllMovies = "SELECT * FROM " + table + ";";
- Use
pool.connect()
to connect to database - Within the response use
client.release()
to open up database for query - Run
return client.query(YOUR_QUERY)
to make your query - Your get function should look like the following:
module.exports.get = (event, context, callback) => {
const getAllStudents = "SELECT * FROM " + table + ";";
pool.connect()
.then(client => {
client.release()
return client.query(getAllStudents)
})
.then(res => {
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true
},
body: JSON.stringify(res.rows),
}
callback(null, response);
})
.catch(error => {
console.log('error', error)
const response = {
statusCode: 500,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true
},
body: JSON.stringify(error)
}
callback(null, response);
});
};
- Invoke function or run GET endpoint in Postman
- Allow permission to your table name
GRANT USAGE, SELECT ON SEQUENCE TABLE_NAME_id_seq TO YOUR_USERNAME;
- Add
integration: lambda
undercors: true
of your post function inserverless.yml
- Create insert query
const postStudent = "INSERT INTO " + table + " Values(default, $1, $2)"
- Pass in values within
pool.connect()
code block with the following
return client.query(postStudent, \[name, grade_level\])
- Your post function should look like the following:
module.exports.post = (event, context, callback) => {
let { name, grade_level } = event.body
const postStudent = "INSERT INTO " + table + " Values(default, $1, $2)"
pool.connect()
.then((client) => {
client.release()
return client.query(postStudent, [name, grade_level])
})
.then((res) => {
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true
},
body: JSON.stringify(res)
}
callback(null, response);
console.log('Your connection will now be terminated')
})
.catch(e => {
console.log('error', e)
const response = {
"statusCode": 500,
"body": JSON.stringify(e)
}
callback(null, response);
});
};
- Implement Put & Delete functions
PostgreSQL: Documentation: 11: DELETE
What Is Amazon Relational Database Service (Amazon RDS)? - Amazon Relational Database Service