Create an API Key with AWS CDK AppSync

Tomasz Łakomy
InstructorTomasz Łakomy
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

The default key that is generated for your AppSync API expires in only 7 days. It would be a big pain in the fingers to update your app every 7 days!

Instead, you can use AppSync to create and configure your own API key, where you can set whatever expiration date you want.

Tomasz Łakomy: [0:00] When we deployed our AppSync API, a default API key was created for us. You can see over here, that it's going to expire seven days after creation, which is not exactly optimal. Imagine that you would like to use this API key in a mobile app. You probably don't want to update this key every seven days. As such, we're going to create our very own API key to use in this API.

[0:22] In order to do that, we're going to use the AuthorizationConfig prop from AWS CDK AppSync construct. I'm going to specify a default authorization, which is going to be of type, AuthorizationType. I'm going to use the AppSync AuthorizationType.

[0:35] There's a number of authorization types which are supported by AppSync. You can use the API key, you can use the identity and access management from AWS, you can use your very own Lambda authorizer, and, for instance, also a Cognito user pool.

[0:52] In this example, I'm going to keep on using the API key. As such, I'm going to specify an API key config. I'm going to name this API key as my-very-own-api-key. I'm going to set an expiry date. This key expires after CDK expiration. I would like this key to expire after a year. I'm going to do, after cdk.Duration.days(365).

[1:17] As a reminder, the default API key which is created by AppSync, lasts up to seven days. This key is going to last for 365 days, which is of course, a whole year. Next, let's deploy this update. To do that, I'm going to open a terminal and run, cdk deploy.

[1:34] After a successful deployment, we can go back to AWS Console to the Settings tab in our AppSync API. If we scroll down a bit, we're going to see that this particular key expires in the year of 2022, which at the time of this recording, is next year.

[1:48] Let's verify if this API key still works. In order to do that, let us copy this API key. I'm going to navigate back to the GraphQL Playground. I'm going to paste this API key over here and hit Play again. We're going to see that it works fine, and I'm able to fetch the data from the API.

egghead
egghead
~ 7 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today