Skip to content

Commit 3b0b10f

Browse files
committed
ADDED: Deployment to custom domain with custom base path (see Issue #8).
1 parent 8159859 commit 3b0b10f

File tree

5 files changed

+103
-3
lines changed

5 files changed

+103
-3
lines changed

README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This sample uses the [Serverless Application Framework](https://serverless.com/)
77
[![Coverage Status](https://coveralls.io/repos/github/balassy/aws-lambda-typescript/badge.svg)](https://coveralls.io/github/balassy/aws-lambda-typescript)
88
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/balassy/aws-lambda-typescript/master/LICENSE)
99
[![GitHub issues](https://img.shields.io/github/issues/balassy/aws-lambda-typescript.svg)](https://github.com/balassy/aws-lambda-typescript/issues)
10+
[![Swagger Validator](https://img.shields.io/swagger/valid/2.0/https/serverless-sample.balassy.me/api/swagger.json.svg)](https://app.swaggerhub.com/apis/balassy/serverless-sample)
1011

1112
[![Dependencies](https://david-dm.org/balassy/aws-lambda-typescript/status.svg)](https://david-dm.org/balassy/aws-lambda-typescript)
1213
[![DevDependencies](https://david-dm.org/balassy/aws-lambda-typescript/dev-status.svg)](https://david-dm.org/balassy/aws-lambda-typescript#type=dev)
@@ -19,13 +20,14 @@ This sample uses the [Serverless Application Framework](https://serverless.com/)
1920

2021
- Full [TypeScript](https://www.typescriptlang.org/) codebase with strict type annotation - _get as many compile time errors as possible._
2122
- Deployment to AWS from the command line with [Serverless](https://serverless.com/) - _just run an npm script._
23+
- Publishing to your custom [Route53](https://aws.amazon.com/route53/) domain name - _for API URLs that live forever._
2224
- Automated builds with [Travis CI](https://travis-ci.org/) - _get early feedback for every change_.
2325
- Offline execution - _call your endpoints without deploying them to AWS._
2426
- Minimal IAM policy to follow the principle of least privilege - _because with great power comes great responsibility_.
2527
- Code analysis with [TSLint](https://palantir.github.io/tslint/) - _avoid dumb coding mistakes._
2628
- Unit testing with [Mocha](https://mochajs.org/), mocking with [ts-mockito](https://github.com/NagRock/ts-mockito) - _be free to change your implementation._
2729
- Test coverage report with [Istanbul](https://istanbul.js.org/) and [Coveralls](https://coveralls.io) - _so you know your weak spots._
28-
- Generated [Swagger](https://swagger.io/) documentation for the endpoints - _the expected description of your API._
30+
- Generated [Swagger](https://swagger.io/) documentation for the endpoints, which works well with [SwaggerHub](https://app.swaggerhub.com) - _the expected description of your API._
2931
- Multiple layers in the code to separate concerns and independently test them - _avoid monolith and complexity._
3032
- Dependency checks with [David](https://david-dm.org/) and [BitHound](https://www.bithound.io/) - _because the majority of your app is not your code._
3133
- Sample CRUD implementation (in progress) - _to see it all in action_.
@@ -75,9 +77,22 @@ npm install
7577
6. **Customize the name of your service** by changing the following line in the `serverless.yml` file:
7678

7779
```
78-
service: serverless-lambda-typescript-example
80+
service: serverless-sample
7981
```
8082

83+
7. **Customize the name of your domain** by changing the following lines in the `serverless.yml` file:
84+
85+
```
86+
custom:
87+
customDomain:
88+
domainName: serverless-sample.balassy.me
89+
certificateName: serverless-sample.balassy.me
90+
```
91+
92+
**NOTE:** You must have the certificate created in [AWS Certificate Manager](https://aws.amazon.com/certificate-manager/) before executing this command. According to AWS to use an ACM certificate with API Gateway, you must [request or import the certificate](https://serverless.com/blog/serverless-api-gateway-domain/) in the US East (N. Virginia) region.
93+
94+
If you don't want to publish your API to a custom domain, remove the `serverless-domain-manager` from the `plugins` section, and the `customDomains` entry from the `custom` section of the `serverless.yml` file.
95+
8196
## What you can find in the code
8297

8398
### Example CRUD endpoints
@@ -102,6 +117,8 @@ To understand the code, open `src/cities/cities.ts`, find the `getCity` function
102117

103118
The `src/swagger` folder contains the `/swagger.json` endpoint which exports the documentation of the API in [Swagger](https://swagger.io/) format. Call the endpoint after deploying your API and paste the response JSON into the [Swagger Editor](https://editor.swagger.io) to display it in a friendly way.
104119

120+
You can also reference the `swagger.json` URL when you publish your documentation via [SwaggerHub](https://app.swaggerhub.com), as you can see on the SwaggerHub page of this sample: https://app.swaggerhub.com/apis/balassy/serverless-sample.
121+
105122
## Developer tasks
106123

107124
### Test the service locally
@@ -124,6 +141,14 @@ You can modify your code after running this command, Serverless will automatical
124141

125142
### Deploy to AWS
126143

144+
**To create a custom domain for your service in AWS, run this command once:** _This command requires Administrator privileges on Windows!_
145+
146+
```bash
147+
npm run deploy:init
148+
```
149+
150+
According to AWS, after this command it may take up to 40 minutes to initialize the domain with a CloudFront distribution. In practice it usually takes about 10 minutes.
151+
127152
**To deploy the service to AWS, run:** _This command requires Administrator privileges on Windows!_
128153

129154
```bash
@@ -138,6 +163,12 @@ npm run deploy
138163

139164
Verify that the deployment is completed successfully by opening the URL displayed in your console window in your browser. To see all resources created in AWS navigate to CloudFormation in the AWS Console and look for the stack named with the name of your service you specified in Step 6.
140165

166+
**To download the Swagger description** of your service, open the following URL in your browser:
167+
168+
```
169+
https://<your_custom_domain_name>/api/swagger.json
170+
```
171+
141172
If you don't want to deploy your code, just want to peek into the deployment package, you can run:
142173

143174
```bash

aws-setup/aws-policy.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,34 @@
142142
"events:Describe*"
143143
],
144144
"Resource": "arn:aws:events::<account_no>:rule/<service_name>*"
145+
},
146+
{
147+
"Effect": "Allow",
148+
"Action": [
149+
"acm:ListCertificates"
150+
],
151+
"Resource": "arn:aws:acm:<region>:<account_no>:certificate/*"
152+
},
153+
{
154+
"Effect": "Allow",
155+
"Action": [
156+
"route53:ListHostedZones"
157+
],
158+
"Resource": "*"
159+
},
160+
{
161+
"Effect": "Allow",
162+
"Action": [
163+
"route53:ChangeResourceRecordSets"
164+
],
165+
"Resource": "arn:aws:route53:::hostedzone/<hosted_zone_ID>/rrset"
166+
},
167+
{
168+
"Effect": "Allow",
169+
"Action": [
170+
"cloudfront:UpdateDistribution"
171+
],
172+
"Resource": "*"
145173
}
146174
]
147175
}

package-lock.json

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"build": "serverless package",
1212
"predeploy": "npm run clean && npm run analyse",
1313
"deploy": "serverless deploy",
14+
"deploy:init": "serverless create_domain",
1415
"lint": "tslint --project . --format verbose",
1516
"start": "serverless offline start",
1617
"pretest": "npm run clean",
@@ -43,6 +44,7 @@
4344
"nyc": "11.2.1",
4445
"rimraf": "2.6.2",
4546
"serverless-aws-documentation": "0.8.0",
47+
"serverless-domain-manager": "1.1.12",
4648
"serverless-offline": "3.15.3",
4749
"serverless-plugin-typescript": "1.1.2",
4850
"source-map-support": "0.4.18",

serverless.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
service: serverless-lambda-typescript-example
1+
service: serverless-sample
22

33
provider:
44
name: aws
@@ -16,6 +16,7 @@ plugins:
1616
- serverless-plugin-typescript
1717
- serverless-offline
1818
- serverless-aws-documentation
19+
- serverless-domain-manager
1920

2021
package:
2122
include:
@@ -77,4 +78,10 @@ functions:
7778

7879
custom:
7980
documentation: ${file(./swagger/documentation.yml)}
81+
customDomain:
82+
basePath: "api"
83+
domainName: serverless-sample.balassy.me
84+
certificateName: serverless-sample.balassy.me
85+
stage: ${self:provider.stage}
86+
createRoute53Record: true
8087

0 commit comments

Comments
 (0)