Learn to Build Serverless Apps

Subcribe for tips on building serverless apps with React & AWS

Extend an AppSync API Key expiration during CloudFormation deployment

All AppSync API keys have an expiration time between 1 and 365 days from when they are created. After creation you can extend the API key but you cannot exceed 365 days in the future. If you need an API key to remain active this isn't ideal because it's only a matter of time until someone forgets to extend the API key.

To get around this I started extending the API keys automatically during CloudFormation deployments.

The first step is to calculate the future expiration time in seconds from epoch. This can be done using the Unix date command. I use this to set an environment variable with the new expiration time.

export API_KEY_EXPIRES=`date -v+365d "+%s"`

Now I have the future expiration time it can be passed into the CloudFormation template during deployment. I'm using SAM CLI so it looks like:

sam deploy
  --template-file template.yaml
  --stack-name my-api
  --parameter-overrides ApiKeyExpires=${API_KEY_EXPIRES}

Inside the CloudFormation template you need have ApiKeyExpires as a parameter.

    Type: Number
    Description: API key expiration time in seconds past epoch

Finally you can set the expires time for the API key in CloudFormation.

    Type: AWS::AppSync::ApiKey
          - GraphQLAPI
          - ApiId
      Description: A really important API that should never expire
        Ref: ApiKeyExpires

Now your API key expiration time will be updated to 365 days in the future during each deployment.

Learn to Build Serverless Apps

Do you want to learn how to build servless apps? Subscribe to my mailing list to get receive updates and articles that will help you build your first serverless app.

BONUS You'll also receive early access and discounts to my new AppSync course.

Build Serverless Apps with AppSync and Amplify

I'm writing a course on building applications with Amplify and AppSync. Subscribe today to receive updates and preview videos.

EmailTwitterGithubYouTubeFacebookFacebookLinkedinRSS Feed

Building serverless apps using TypeScript/JavaScript, AWS Amplify and React with an Amazon Web Services (AWS) backend (Lambda, AppSync, API Gateway, S3, DynamoDB, SNS and more).