Skip to content

Serverless Infrastructure - IaC: AWS CDK - AWS Services (Amplify + Cognito + AppSync + Lambda + IAM + VPC + EC2 + SM + RDS ) + Server Logic (Node + Prisma-ORM <-> PostgreSQL)

Notifications You must be signed in to change notification settings

kwenamokgohlwa/foodo-server-v1

Repository files navigation

CDK API with GraphQL and Aurora Serverless PostgreSQL and Prisma

This is a fork of a repo which was livestreamed by Nader Dabit that is modified to use Prisma.

Warning: The stack is not free

This stack will not come under the free tier. To delete all resources after you're done, use the following command:

yarn cdk destroy --profile profileName

Where the profileName is your AWS profile name. I have used prisma_demo here but you can replace that with any profile.

This CDK stack deploys a real-time GraphQL API built with AWS AppSync, Amazon Aurora Serverless PostgreSQL, AWS Lambda, and Prisma.

CDK API with GraphQL, Aurora Serverless PostgreSQL, and Prisma

Getting started

To deploy this project, follow these steps.

1. Clone the project

git clone https://github.com/ryands17/graphql-api-cdk-serverless-postgres.git

2. Change into the new directory and install dependencies

cd graphql-api-cdk-serverless-postgres
yarn

3. Change into the lambda-fns directory and install the dependencies for the Lambda function package:

cd lambda-fns
yarn
cd ..

4. Initialize cdk.context.json in the root directory with the following values (The account ID should match the one you're deploying to)

{
  "region": "us-east-1",
  "accountID": "123456789012",
  "keyName": "sshKeyName"
}

5. Deploy the stack

Note: I have deployed using an AWS Profile named default. To use your own profile, add the profile option after the deploy command like this:

yarn deploy --profile profile_name

Learn more about how to create an AWS profile here.

yarn deploy

6. Create the posts table

Visit the RDS dashboard and click on Query Editor. From the dropdown menu, choose the database (it should begin with appsynccdkrdsstack-aurorablogcluster).

For the Database username, choose Connect with a Secrets Manager ARN.

To sign in, you will need the ARN from the secret that was created by CDK. To get this secret, in a new window open AWS Secrets manager. Here, click on the secret that was created by CDK (it should start with AuroraBlogClusterSecret). Copy the Secret ARN to your clipboard and go back to the RDS Query Editor.

Next, use the Secret ARN as the Secrets Manager ARN and BlogDB as the name of the database. Next, press enter and click on Connect to Database.

Once signed in, create the posts table by executing the following query:

create table posts (
 id text primary key,
 title text,
 content text
);

7. Testing the API

Next, visit the AppSync console and click on cdk-blog-appsync-api to view the dashboard for your API.

Next click on Queries in the left hand menu to view the query editor. From here, we can test out the API by running the following queries and mutations:

mutation createPost {
  createPost(
    post: { id: "001", title: "My first post!", content: "Hello world!" }
  ) {
    id
    title
    content
  }
}

query listPosts {
  listPosts {
    id
    title
    content
  }
}

query getPostById {
  getPostById(postId: "001") {
    id
    title
    content
  }
}

mutation updatePost {
  updatePost(post: { id: "001", title: "My updated post!" }) {
    id
    title
  }
}

mutation deletePost {
  deletePost(postId: "001")
}

About

Serverless Infrastructure - IaC: AWS CDK - AWS Services (Amplify + Cognito + AppSync + Lambda + IAM + VPC + EC2 + SM + RDS ) + Server Logic (Node + Prisma-ORM <-> PostgreSQL)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published