You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: USE_CASES.md
+175
Original file line number
Diff line number
Diff line change
@@ -8,6 +8,7 @@ This documentation provides examples for specific use cases. Please [open an iss
8
8
*[How to View Email Statistics](#email_stats)
9
9
*[Asynchronous Mail Send](#asynchronous-mail-send)
10
10
*[Error Handling](#error-handling)
11
+
*[Deploy A Simple Hello Email App on AWS](#hello_email_on_aws)
11
12
12
13
<aname="transactional-templates"></a>
13
14
# Transactional Templates
@@ -300,3 +301,177 @@ Please see [here](https://github.com/sendgrid/python-http-client/blob/master/pyt
300
301
print(response.body)
301
302
print(response.headers)
302
303
```
304
+
305
+
<aname="hello_email_on_aws"></a>
306
+
# Deploy a simple Hello Email app on AWS
307
+
308
+
This tutorial explains how to set up a simple "Hello Email" app on AWS, using the AWS CodeStar service.
309
+
310
+
We'll be creating a basic web service to send email via SendGrid. The application will run on AWS Lambda, and the "endpoint" will be via AWS API Gateway.
311
+
312
+
The neat thing is that CodeStar provides all of this in a pre-configured package. We just have to make some config changes, and push our code.
313
+
314
+
Once this tutorial is complete, you'll have a basic web service for sending email that can be invoked via a link to your newly created API endpoint.
315
+
316
+
### Prerequisites
317
+
Python 2.6, 2.7, 3.4, or 3.5 are supported by the sendgrid Python library, however I was able to utilize 3.6 with no issue.
318
+
319
+
Before starting this tutorial, you will need to have access to an AWS account in which you are allowed to provision resources. This tutorial also assumes you've already created a SendGrid account with free-tier access. Finally, it is highly recommended you utilize [virtualenv](https://virtualenv.pypa.io/en/stable/).
320
+
321
+
*DISCLAIMER*: Any resources provisioned here may result in charges being incurred to your account. Sendgrid is in no way responsible for any billing charges.
322
+
323
+
324
+
## Getting Started
325
+
326
+
### Create AWS CodeStar Project
327
+
Log in to your AWS account and go to the AWS CodeStar service. Click "Start a project". For this tutorial we're going to choose a Python Web service, utilizing AWS Lambda. You can use the filters on the left hand side of the UI to narrow down the available choices.
328
+
329
+
After you've selected the template, you're asked to provide a name for your project. Go ahead and name it "hello-email". Once you've entered a name, click "Create Project" in the lower right hand corner. You can then choose which tools you want to use to interact with the project. For this tutorial, we'll be choosing "Command Line".
330
+
331
+
Once that is completed, you'll be given some basic steps to get Git installed and setup, and instructions for connecting to the AWS CodeCommit(git) repository. You can either use HTTPS, or SSH. Instructions for setting up either are provided.
332
+
333
+
Go ahead and clone the Git repository link after it is created. You may need to click "Skip" in the lower right hand corner to proceed.
334
+
335
+
Once that's done, you've successfully created a CodeStar project! You should be at the dashboard, with a view of the wiki, change log, build pipeline, and application endpoint.
336
+
337
+
### Create SendGrid API Key
338
+
Log in to your SendGrid account. Click on your user name on the left hand side of the UI and choose "Setup Guide" from the drop-down menu. On the "Welcome" menu, choose "Send Your First Email", and then "Integrate using our Web API or SMTP relay." Choose "Web API" as the recommended option on the next screen, as we'll be using that for this tutorial.
339
+
340
+
On the next menu, you have the option to choose what programming language you'll be using. The obvious choice for this tutorial will be Python.
341
+
342
+
Follow the steps on the next screen. Choose a name for your API key, such as "hello-email". Follow the remaining steps to create an environment variable, install the sendgrid module, and copy the test code. Once that is complete, check the "I've integrated the code above" box, and click the "Next: Verify Integration" button.
343
+
344
+
Assuming all the steps were completed correctly, you should be greeted with a success message. If not, go back and verify that everything is correct, including your API key environment varible, and Python code.
345
+
346
+
## Deploy hello-world app using CodeStar
347
+
348
+
For the rest of the tutorial, we'll be working out of the git repository we cloned from AWS earlier:
349
+
```
350
+
$ cd hello-email
351
+
```
352
+
note: this assumes you cloned the git repo inside your current directory. My directory is:
353
+
354
+
```
355
+
~/projects/hello-email
356
+
```
357
+
358
+
The directory contents should be as follows:
359
+
360
+
├──buildspec.yml
361
+
├──index.py
362
+
├──template.yml
363
+
├──README.md
364
+
365
+
The `buildspec.yml` file is a YAML definition for the AWS CodeBuild service, and will not need to be modified for this tutorial. The `index.py` is where the application logic will be placed, and the `template.yml` is a YAML definition file for the AWS Lambda function.
366
+
367
+
We'll start by modifying the `template.yml` file. Copy and paste from the example below, or edit your existing copy to match:
368
+
369
+
```yaml
370
+
AWSTemplateFormatVersion: 2010-09-09
371
+
Transform:
372
+
- AWS::Serverless-2016-10-31
373
+
- AWS::CodeStar
374
+
375
+
Parameters:
376
+
ProjectId:
377
+
Type: String
378
+
Description: CodeStar projectId used to associate new resources to team members
In the root project directory, run the following commands:
403
+
```
404
+
virtualenv venv
405
+
source ./venv/bin/activate
406
+
```
407
+
408
+
Prior to being able to deploy our Python code, we'll need to install the sendgrid Python module *locally*. One of the idiosyncracies of AWS Lambda is that all library and module dependencies that aren't part of the standard library have to be included with the code/build artifact. Virtual environments do not translate to the Lambda runtime environment.
409
+
410
+
In the root project directory, run the following command:
411
+
```
412
+
$ pip install sendgrid -t .
413
+
```
414
+
This will install the module locally to the project dir, where it can be built into the Lambda deployment.
415
+
416
+
Now go ahead and modify the `index.py` file to match below:
Note that for the most part, we've simply copied the intial code from the API verification with SendGrid. Some slight modifications were needed to allow it to run as a lambda function, and for the output to be passed cleanly from the API endpoint.
448
+
449
+
Change the `[email protected]` emails appropriately so that you may receive the test email.
450
+
451
+
Go ahead and commit/push your code:
452
+
453
+
```
454
+
$ git add .
455
+
```
456
+
457
+
```
458
+
$ git commit -m 'hello-email app'
459
+
```
460
+
461
+
```
462
+
$ git push
463
+
```
464
+
465
+
Once the code is successfully pushed, head back to the AWS CodeStar dashboard for your project. After your commit successfully registers, an automated build and deployment process should kick off.
466
+
467
+
One more step left before our application will work correctly. After your code has bee deployed, head to the AWS Lambda console. Click on your function name, which should start with `awscodestar-hello-email-lambda-`, or similar.
468
+
469
+
Scroll down to the "Environment Variables" section. Here we need to populate our SendGrid API key. Copy the value from the `sendgrid.env` file you created earlier, ensuring to capture the entire value. Make sure the key is titled:
470
+
471
+
```
472
+
SENDGRID_API_KEY
473
+
```
474
+
475
+
Now, go back to your project dashboard in CodeStar. Click on the link under "Application endpoints". After a moment, you should be greeted with JSON output indicating an email was successfully sent.
476
+
477
+
Congratulations, you've just used serverless technology to create an email sending app in AWS!
0 commit comments