Skip to content

Commit

Permalink
Updated guide to CDK V2 (#640)
Browse files Browse the repository at this point in the history
* updated guide to cdk V2

* Fix tests

Co-authored-by: Frank <[email protected]>
  • Loading branch information
Manitej66 and fwang authored Jan 21, 2022
1 parent 79c4e7e commit cdb4bd7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 37 deletions.
24 changes: 7 additions & 17 deletions _chapters/adding-auth-to-our-serverless-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ Setting this all up can be pretty complicated in CDK. SST has a simple [`Auth`](

{%change%} Add the following to a new file in `stacks/AuthStack.js`.

``` js
import * as iam from "@aws-cdk/aws-iam";
```js
import * as iam from "aws-cdk-lib/aws-iam";
import * as sst from "@serverless-stack/resources";

export default class AuthStack extends sst.Stack {
Expand Down Expand Up @@ -80,21 +80,11 @@ Let's quickly go over what we are doing here.

- Finally, we output the ids of the auth resources that've been created.

We also need to install a CDK package for the IAM policy that we are creating.

{%change%} Run the following in your project root.

``` bash
$ npx sst add-cdk @aws-cdk/aws-iam
```

We are using this command instead of `npm install` because there's [a known issue with CDK](https://docs.serverless-stack.com/known-issues) where mismatched versions can cause a problem.

### Securing Access to Uploaded Files

We are creating a specific IAM policy to secure the files our users will upload to our S3 bucket.

``` js
```js
// Policy granting access to a specific folder in the bucket
new iam.PolicyStatement({
actions: ["s3:*"],
Expand All @@ -117,7 +107,7 @@ Let's add this stack to our app.

{%change%} Replace the `main` function in `stacks/index.js` with this.

``` js
```js
export default function main(app) {
const storageStack = new StorageStack(app, "storage");

Expand All @@ -136,7 +126,7 @@ Here you'll notice that we are passing in our API and S3 Bucket to the auth stac

{%change%} Also, import the new stack at the top.

``` js
```js
import AuthStack from "./AuthStack";
```

Expand All @@ -146,7 +136,7 @@ We also need to enable authentication in our API.

{%change%} Add the following above the `defaultFunctionProps: {` line in `stacks/ApiStack.js`.

``` js
```js
defaultAuthorizationType: "AWS_IAM",
```

Expand All @@ -160,7 +150,7 @@ Note that, you'll need to have `sst start` running for this to happen. If you ha

You should see something like this at the end of the deploy process.

``` bash
```bash
Stack dev-notes-auth
Status: deployed
Outputs:
Expand Down
14 changes: 7 additions & 7 deletions _chapters/handle-cors-in-serverless-apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
layout: post
title: Handle CORS in Serverless APIs
date: 2021-08-17 00:00:00
lang: en
lang: en
ref: handle-cors-in-serverless-apis
description: In this chapter we'll look at how to configure CORS in our serverless API. We'll be adding these settings in our SST Api construct and in our Lambda function responses.
comments_id: handle-cors-in-serverless-apis/2175
Expand Down Expand Up @@ -38,7 +38,7 @@ There's a bit more to CORS than what we have covered here. So make sure to [chec

If we don't set the above up, then we'll see something like this in our HTTP responses.

``` text
```text
No 'Access-Control-Allow-Origin' header is present on the requested resource
```

Expand All @@ -48,7 +48,7 @@ And our browser won't show us the HTTP response. This can make debugging our API

The SST [`Api`](https://docs.serverless-stack.com/constructs/Api) construct that we are using enables CORS by default.

``` js
```js
new Api(this, "Api", {
// Enabled by default
cors: true,
Expand All @@ -60,8 +60,8 @@ new Api(this, "Api", {

You can further configure the specifics if necessary. You can [read more about this here](https://docs.serverless-stack.com/constructs/Api#cors).

``` js
import { HttpMethod } from "@aws-cdk/aws-apigatewayv2";
```js
import { HttpMethod } from "@aws-cdk/aws-apigatewayv2-alpha";

new Api(this, "Api", {
cors: {
Expand All @@ -81,7 +81,7 @@ Next, we need to add the CORS headers in our Lambda function response.

{%change%} Replace the `return` statement in our `src/util/handler.js`.

``` javascript
```javascript
return {
statusCode,
body: JSON.stringify(body),
Expand All @@ -90,7 +90,7 @@ return {

{%change%} With the following.

``` javascript
```javascript
return {
statusCode,
body: JSON.stringify(body),
Expand Down
25 changes: 12 additions & 13 deletions _chapters/unit-tests-in-serverless.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ref: unit-tests-in-serverless
comments_id: unit-tests-in-serverless/173
---

Our serverless app is made up of two big parts; the code that defines our infrastructure and the code that powers our Lambda functions. We'd like to be able to test both of these.
Our serverless app is made up of two big parts; the code that defines our infrastructure and the code that powers our Lambda functions. We'd like to be able to test both of these.

On the infrastructure side, we want to make sure the right type of resources are being created. So we don't mistakingly deploy some updates.

Expand All @@ -22,8 +22,8 @@ Let's start by writing a test for the CDK infrastructure in our app. We are goin

{%change%} Add the following to `test/StorageStack.test.js`.

``` js
import { expect, haveResource } from "@aws-cdk/assert";
```js
import { Template } from "aws-cdk-lib/assertions";
import * as sst from "@serverless-stack/resources";
import StorageStack from "../stacks/StorageStack";

Expand All @@ -32,11 +32,10 @@ test("Test StorageStack", () => {
// WHEN
const stack = new StorageStack(app, "test-stack");
// THEN
expect(stack).to(
haveResource("AWS::DynamoDB::Table", {
BillingMode: "PAY_PER_REQUEST",
})
);
const template = Template.fromStack(stack);
template.hasResourceProperties("AWS::DynamoDB::Table", {
BillingMode: "PAY_PER_REQUEST",
});
});
```

Expand All @@ -46,7 +45,7 @@ We also have a sample test created with the starter that we can remove.

{%change%} Run the following in your project root.

``` bash
```bash
$ rm test/MyStack.test.js
```

Expand All @@ -56,7 +55,7 @@ We are also going to test the business logic in our Lambda functions.

{%change%} Create a new file in `test/cost.test.js` and add the following.

``` js
```js
import { calculateCost } from "../src/util/cost";

test("Lowest tier", () => {
Expand Down Expand Up @@ -93,13 +92,13 @@ This should be straightforward. We are adding 3 tests. They are testing the diff

And we can run our tests by using the following command in the root of our project.

``` bash
```bash
$ npx sst test
```

You should see something like this:

``` bash
```bash
PASS test/cost.test.js
PASS test/StorageStack.test.js

Expand All @@ -116,7 +115,7 @@ And that's it! We have unit tests all configured. These tests are fairly simple

{%change%} Let's commit our changes and push it to GitHub.

``` bash
```bash
$ git add .
$ git commit -m "Adding unit tests"
$ git push
Expand Down

0 comments on commit cdb4bd7

Please sign in to comment.