Git Product home page Git Product logo

amplify2terraform's Introduction

amplify2terraform

This repo is a proof-of-concept for using Amplify client libraries with Terraform (and serverless for Lambda functions) rather than Amplify CLI and Console.

See my blog for an explanation of why: https://tommygroshong.com/posts/appsync-cognito-cloudfront/

Prereqs

Install the following tools:

Setup AWS API credentials: aws configure

Build

The build workflow is as follows:

  • Terraform infrastructure:
    • cd terraform
    • terraform init (only needed the first time)
    • terraform apply
  • Deploy serverless function:
    • cd serverless
    • serverless deploy -v
  • Build and push react app:
    • terraform output -state=terraform/terraform.tfstate aws-exports-file > src/aws-exports.js
    • yarn build
    • aws s3 sync build s3://<YOURBUCKET> --cache-control max-age=300

Overview

Infrastructure

My terraform scripts are in terraform/ and the resources are organized thusly:

  1. main.tf: Variables, Outputs, and Data resources
  2. api.tf: DynamoDB; AppSync API, Data Sources, and Resolvers; IAM perms
  3. auth.tf: Cognito Identity Pool, User Pool, User Groups, Clients, and IAM roles
  4. website.tf: S3 website bucket, CloudFront distribution, Route 53 Record

Serverless functions are in services/. The code is just a dummy example. The serverless.yml however shows a few useful examples:

  1. Giving access to DynamoDB tables
  2. Outputting your function ARNs so you can plug them into terraform as variables

When using these scripts in a non-toy app, I create multiple terraform workspaces and separate <stage>.tfvars files for use with each. Not beautiful, but works.

Client

The client app was bootstrapped with Create React App.

It uses the AppSyncClientSDK which is an Apollo compatible client; just pass it to your provider: <ApolloProvider client={client} />.

The pattern that Amplify and AppSync give you for configuring the Clients is having a src/aws-exports.js file that is ignored from git and contains all your configuration details for AppSync, Cognito, etc. My code does the same pattern except uses terraform to generate that file:

terraform output aws-exports-file > ../src/aws-exports.js

That was a trick I picked up for generating Kubeconfig files after setting up Kubernetes. So after creating your infrastructure with terraform, you should generate the latest aws-exports.js file.

NOTE: Generate the latest aws-exports.js file for your target stage before bundling code!

Your bundled code will essentially be hardcoded to point at whatever stage/environment was referenced by aws-exports.js at the time of bundling. Notice in the instructions above on how to build and push react app it has you write the src/aws-exports.js first. Those few lines are should go in a deploy script.

amplify2terraform's People

Contributors

dependabot[bot] avatar tgroshon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

amplify2terraform's Issues

State of the project

๐Ÿ‘‹ @tgroshon ,

I am trying to prototype a backend infrastructure based on mostly terraform. I wanted to see your current situation about lambdas. Are you still using serverless framework? I will be glad that if you provide more information about it. Thank for sharing project, it is inspiring.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.