<commit_before># AWS Lambda Reference Architecture: Mobile Backend

## Introduction

The Mobile Backend reference architecture ([diagram](https://s3.amazonaws.com/awslambda-reference-architectures/mobile-backend/lambda-refarch-mobilebackend.pdf)) demonstrates how to use AWS Lambda along with other services to build a serverless backend for a mobile application. The specific example application provided enables users to upload photos and notes via S3 and API Gateway respectively. The notes are stored in Amazon DynamoDB and are processed asynchronously using DynamoDB streams and a Lambda function in order to add them to an Amazon CloudSearch domain. In addition to the source code for the Lambda functions, this repository also contains a prototype iOS application that provides examples for how to use the AWS mobile SDKs to interface with the backend resources defined in the architecture.

## Running the example

To run the full example application you must first deploy the backend resources and then compile and run the example iOS application.

### Deploying the Backend

The provided AWS CloudFormation template will create most of the backend resources necessary for this example, but you will need to create the Amazon CloudSearch domain, API Gateway REST API, and Cognito identity pool outside of AWS CloudFormation.

#### Step 1. Create Amazon CloudSearch Domain

1. Create a new Amazon CloudSearch domain using the [AWS CLI](https://aws.amazon.com/cli/) providing a domain name of your choice.
```
aws cloudsearch create-domain --domain-name [YOUR_DOMAIN_NAME]
```

1. Note the ARN of the new domain in the output document. You will use this as an input when launching the CloudFormation stack.

1. Define indexes for the `headline` and `text` fields.
```
aws cloudsearch define-index-field --name headline --type text --domain-name [YOUR_DOMAIN_NAME]
aws cloudsearch define-index-field --name text --type text --domain-name [YOUR_DOMAIN_NAME]
```

#### Step 2. Create an API Gateway REST API

1. Using the [AWS CLI](https://aws.amazon.com/cli/), create a new API providing a name of your choice.
```
aws apigateway create-rest-api --name [YOUR_API_NAME]
```

1. Note the API ID provided in the output document. You will use this as an input when launching the CloudFormation stack.

#### Step 3. Create a Cognito Identity Pool

1. Create a new identity pool using the [AWS CLI](https://aws.amazon.com/cli/) providing a name of your choice.

```
aws cognito-identity create-identity-pool --allow-unauthenticated-identities --identity-pool-name [YOUR_POOL_NAME]
```

2. Note the IdentityPoolId from the output document. You will use this as a parameter when launching the CloudFormation stack.

#### Step 4. Launch the AWS CloudFormation template

Use the following link to launch the provided AWS CloudFormation template in your account.

[![Launch Lambda Mobile Backend into North Virginia with CloudFormation](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/images/cloudformation-launch-stack-button.png)](https://console.aws.amazon.com/cloudformation/home?region=us-east-1#/stacks/new?stackName=lambda-mobile-backend&templateURL=https://s3.amazonaws.com/awslambda-reference-architectures/mobile-backend/mobile-backend.template)

*Note:* The provided template can be launched in the us-east-1 region using the provided bucket. If you would like to launch this stack in a different region you must create an Amazon S3 bucket in that region and copy the template and Lambda function definitions into it.

When prompted enter the parameter values for the CloudSearch domain, API Gateway REST API, and Cognito identity pool resources you created in the previous steps.

#### Step 5. Update your API Gateway REST API

Once the CloudFormation stack has been created, you need to update the API you created previously to use the newly created `NotesApiFunction`.

1. Select your API in the [Amazon API Gateway Console](https://console.aws.amazon.com/apigateway/home?region=us-east-1#/apis).
1. Click *Create Resource* to create a new child resource under /.
1. Enter `notes` as the resource name and `/notes` as the resource path.
1. Click *Create Resource*.
1. With the new `/notes` resource selected, click *Create Method*.
1. Select `POST` and click the checkbox.
1. Select *Lambda Function* as the integration type and the region where you launched the CloudFormation stack as the Lambda region.
1. Type 'NotesApiFunction' in the *Lambda Function* input box and select the function crated by the CloudFormation Stack.
1. Click *Save* and grant API Gateway permission to execute the Lambda function.
1. Click Method Request to edit the request configuration.
1. Set the *Authorization type* to `AWS_IAM` and set *API Key Required* to `true`. Click the check mark next to each field.
1. Click *Deploy API*.
1. Select `New Stage` for *Deployment stage* and provide a name in the *Stage name* field.
1. Note the *Invoke URL* for the new stage. You will use this value when running the sample iOS app.

#### Step 6. Update your Cognito Identity Pool

1. Select your identity pool in the [Amazon Cognito Console](https://console.aws.amazon.com/cognito/home?region=us-east-1).
1. Click *Edit Identity Pool*
1. For both the *Unauthenticated role* and the *Authenticated role* select the MobileClientRole created by the CloudFormation stack. The full ARN for the role is provided in the outputs of the stack.
1. Click *Save Changes*


### Running the sample iOS application

#### Prerequisites

To run the provided iOS sample application you must be running *Mac OS X 10.10 (Yosemite)* or higher. You must also have the latest version of *[Xcode](https://itunes.apple.com/us/app/xcode/id497799835)* and *[Cocoa Pods](https://cocoapods.org/)* installed. 


[Template One](https://s3.amazonaws.com/awslambda-reference-architectures/mobile-backend/mobiledatastore.template)
does the following:
<commit_msg>switching italics to bold<commit_after># AWS Lambda Reference Architecture: Mobile Backend

## Introduction

The Mobile Backend reference architecture ([diagram](https://s3.amazonaws.com/awslambda-reference-architectures/mobile-backend/lambda-refarch-mobilebackend.pdf)) demonstrates how to use AWS Lambda along with other services to build a serverless backend for a mobile application. The specific example application provided enables users to upload photos and notes via S3 and API Gateway respectively. The notes are stored in Amazon DynamoDB and are processed asynchronously using DynamoDB streams and a Lambda function in order to add them to an Amazon CloudSearch domain. In addition to the source code for the Lambda functions, this repository also contains a prototype iOS application that provides examples for how to use the AWS mobile SDKs to interface with the backend resources defined in the architecture.

## Running the example

To run the full example application you must first deploy the backend resources and then compile and run the example iOS application.

### Deploying the Backend

The provided AWS CloudFormation template will create most of the backend resources necessary for this example, but you will need to create the Amazon CloudSearch domain, API Gateway REST API, and Cognito identity pool outside of AWS CloudFormation.

#### Step 1. Create Amazon CloudSearch Domain

1. Create a new Amazon CloudSearch domain using the [AWS CLI](https://aws.amazon.com/cli/) providing a domain name of your choice.
```
aws cloudsearch create-domain --domain-name [YOUR_DOMAIN_NAME]
```

1. Note the ARN of the new domain in the output document. You will use this as an input when launching the CloudFormation stack.

1. Define indexes for the `headline` and `text` fields.
```
aws cloudsearch define-index-field --name headline --type text --domain-name [YOUR_DOMAIN_NAME]
aws cloudsearch define-index-field --name text --type text --domain-name [YOUR_DOMAIN_NAME]
```

#### Step 2. Create an API Gateway REST API

1. Using the [AWS CLI](https://aws.amazon.com/cli/), create a new API providing a name of your choice.
```
aws apigateway create-rest-api --name [YOUR_API_NAME]
```

1. Note the API ID provided in the output document. You will use this as an input when launching the CloudFormation stack.

#### Step 3. Create a Cognito Identity Pool

1. Create a new identity pool using the [AWS CLI](https://aws.amazon.com/cli/) providing a name of your choice.

```
aws cognito-identity create-identity-pool --allow-unauthenticated-identities --identity-pool-name [YOUR_POOL_NAME]
```

2. Note the IdentityPoolId from the output document. You will use this as a parameter when launching the CloudFormation stack.

#### Step 4. Launch the AWS CloudFormation template

Use the following link to launch the provided AWS CloudFormation template in your account.

[![Launch Lambda Mobile Backend into North Virginia with CloudFormation](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/images/cloudformation-launch-stack-button.png)](https://console.aws.amazon.com/cloudformation/home?region=us-east-1#/stacks/new?stackName=lambda-mobile-backend&templateURL=https://s3.amazonaws.com/awslambda-reference-architectures/mobile-backend/mobile-backend.template)

**Note:** The provided template can be launched in the us-east-1 region using the provided bucket. If you would like to launch this stack in a different region you must create an Amazon S3 bucket in that region and copy the template and Lambda function definitions into it.

When prompted enter the parameter values for the CloudSearch domain, API Gateway REST API, and Cognito identity pool resources you created in the previous steps.

#### Step 5. Update your API Gateway REST API

Once the CloudFormation stack has been created, you need to update the API you created previously to use the newly created `NotesApiFunction`.

1. Select your API in the [Amazon API Gateway Console](https://console.aws.amazon.com/apigateway/home?region=us-east-1#/apis).
1. Click **Create Resource** to create a new child resource under /.
1. Enter `notes` as the resource name and `/notes` as the resource path.
1. Click **Create Resource**.
1. With the new `/notes` resource selected, click **Create Method**.
1. Select `POST` and click the checkbox.
1. Select **Lambda Function** as the integration type and the region where you launched the CloudFormation stack as the Lambda region.
1. Type 'NotesApiFunction' in the **Lambda Function** input box and select the function crated by the CloudFormation Stack.
1. Click **Save** and grant API Gateway permission to execute the Lambda function.
1. Click Method Request to edit the request configuration.
1. Set the **Authorization type** to `AWS_IAM` and set **API Key Required** to `true`. Click the check mark next to each field.
1. Click **Deploy API**.
1. Select `New Stage` for **Deployment stage** and provide a name in the **Stage name** field.
1. Note the **Invoke URL** for the new stage. You will use this value when running the sample iOS app.

#### Step 6. Update your Cognito Identity Pool

1. Select your identity pool in the [Amazon Cognito Console](https://console.aws.amazon.com/cognito/home?region=us-east-1).
1. Click **Edit Identity Pool**.
1. For both the **Unauthenticated role** and the **Authenticated role** select the MobileClientRole created by the CloudFormation stack. The full ARN for the role is provided in the outputs of the stack.
1. Click **Save Changes**.


### Running the sample iOS application

#### Prerequisites

To run the provided iOS sample application you must be running **Mac OS X 10.10 (Yosemite)** or higher. You must also have the latest version of **[Xcode](https://itunes.apple.com/us/app/xcode/id497799835)** and **[Cocoa Pods](https://cocoapods.org/)** installed. 


[Template One](https://s3.amazonaws.com/awslambda-reference-architectures/mobile-backend/mobiledatastore.template)
does the following:

{'fraction_non_alphanumeric': 0.07928775407357634, 'fraction_numerical': 0.008903074080295649, 'mean_word_length': 4.627126654064273, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 22, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '30904', 'n_tokens_mistral': 3208, 'n_tokens_neox': 3061, 'n_words': 1549}