29
loading...
This website collects cookies to deliver better user experience
🚨 This project makes use of lambda environment variables. The ability to do this via the CLI was introduced in version 5.1.0
. You may need to run npm install -g @aws-amplify/cli
to ensure you're on the latest version.
contact-form-starter
branch from this github url.// visit: https://github.com/mtliendo/amplify-email-recipes/tree/contact-form-starter
git clone git@github.com:mtliendo/amplify-email-recipes.git
cd amplify-email-recipes
git checkout contact-form-starter
npm install
npm run dev
localhost:3000
and you should be presented with the following screen:AppSync: Fully managed GraphQL API
DynamoDB: NoSQL database
Lambda: FaaS/cloud function
amplify init
n
to deny the default configuration. This is because we will be deploying our application as a static site. In NextJS, the name of that build directory is called out
.Distribution Directory Path
enter out
.AWS profile
we'll choose the profile we'd like to use.amplify add api
GraphQL
[enter] to accept the default name
API key
"Contact form public API"
[enter] to accept a default of 7 days
[enter] to accept "No, I am done."
[enter] to accept the default "N" option
[enter] for a Single object with fields
"y" to edit the schema now
Select your editor of choice
schema.graphql
with a sample Todo object. Replace everything in that file with the following:type Candidate
@model
@auth(rules: [{ allow: public, operations: [create] }]) {
id: ID!
name: String!
email: AWSEmail!
}
Candidate
. In our application, a Candidate represents the user submitting their information.@model
text is called a directive. When Amplify sees this, it will automatically create a DynamoDB table and create CRUDL operations for the type it's associated with (in this case, Candidate).@auth
directive setups up authorization rules on our API. Here we are saying, "We want our API to be public to anyone with an API key, but we only want them to be able to create entries in out database, they can't read, update, or delete items.ID
), a name, and an email -- AWS has a primitive called AWSEmail that automatically validates an email pattern./pets
, but in our application, we want this function to be called whenever an item is added to our database.amplify add function
Lambda function (serverless function)
"contactformuploader" as the name of the function
NodeJS
Lambda Trigger
Amazon DynamoDB Stream
Use API category graphql @model backed DynamoDB table in the current Amplify project
[enter] to not configure advanced settings
[enter] to edit the local function now
Choose your editor of choice
INSERT
, MODIFY
, or REMOVE
event, that information is sent as a stream of data to our lambda function. event.Records
.const aws = require('aws-sdk')
const ses = new aws.SES()
exports.handler = async (event) => {
for (const streamedItem of event.Records) {
if (streamedItem.eventName === 'INSERT') {
//pull off items from stream
const candidateName = streamedItem.dynamodb.NewImage.name.S
const candidateEmail = streamedItem.dynamodb.NewImage.email.S
await ses
.sendEmail({
Destination: {
ToAddresses: [process.env.SES_EMAIL],
},
Source: process.env.SES_EMAIL,
Message: {
Subject: { Data: 'Candidate Submission' },
Body: {
Text: { Data: `My name is ${candidateName}. You can reach me at ${candidateEmail}` },
},
},
})
.promise()
}
}
return { status: 'done' }
}
event
will contain the related stream of data. So from here our job is simple:sendEmail
function from the ses
module.Our function doesn't have permission to interact with SES
We need to setup this process.env.SES_EMAIL
variable
We've yet to setup SES
Our frontend code isn't setup to interact with our backend.
We can only send/receive to verified email addresses
We can only send 1 email/sec
Only 200 emails/day are allowed
amplify console
📝 you may be asked to log in to your AWS account
Click the orange "Create identity" button
Select the "Email address" option and enter your desired email
Click the orange "Create identity" button
SES_EMAIL
.amplify/backend/function/your-function-name/
src
directory for lambda, and a file titledyour-function-name-cloudformation-template.json
lambdaexecutionpolicy
(it should be right around line 132). Statement
array that currently contains a single object. This object lets our function create logs in AWS.Statement
array and save:{
"Action": ["ses:SendEmail"],
"Effect": "Allow",
"Resource": "the-arn-you-copied-from-ses"
}
sendEmail
function using the email we verified.lambdaexecutionpolicy
object should look like the below screenshot (note I removed my email in place of a *
for a bit more flexibility):amplify update function
Lambda function (serverless function)
[Select your function name]
Environment variables configuration
type SES_EMAIL
Enter the email that was verified with SES
I'm done
No -- I don't want to edit the local file now
amplify push
Yes -- generate code for the API 🔥
JavaScript
[enter] to allow the default file path
Yes -- generate all possible operations (recall we only allow create
per our schema)
[enter] to accept a max depth of 2
npm i aws-amplify
_app.js
, add the following lines:import Amplify from '@aws-amplify/core'
import config from '../src/aws-exports'
Amplify.configure(config)
.gitignore
), and then we pass in our config to Amplify.ContactForm.js
, we'll also bring in the following imports:import { API } from 'aws-amplify'
import { createCandidate } from '../src/graphql/mutations'
📝 Feel free to check out the createCandidate
mutations file that Amplify generated for us.
ContactForm.js
file has the following lines of code:// TODO: Add code to send email here
console.log('<send to backend here>')
await API.graphql({
query: createCandidate,
variables: {
input: {
name,
email,
},
},
})
localhost:3000
and test it out. If all went well, after a few seconds you'll have an email in your inbox 🎉 📝 Because our emails are being sent via SES, they may show up in a spam folder or flagged by your email provider. This is because we haven't setup DKIM with SES. Though not terribly difficult, it's out of scope for this tutorial. However, if interested, you can read more about it here
amplify add hosting
Hosting with Amplify Console
Manual Deployment
amplify publish
Copy and paste the generated URL in your terminal to browser to view.
29