<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Erika Tan</title>
    <description>The latest articles on DEV Community by Erika Tan (@erikatstripe).</description>
    <link>https://dev.to/erikatstripe</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F906087%2F71482a97-c66f-428a-96b7-a5f0fd44c5fb.jpeg</url>
      <title>DEV Community: Erika Tan</title>
      <link>https://dev.to/erikatstripe</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/erikatstripe"/>
    <language>en</language>
    <item>
      <title>Deploy a Stripe sample to a cloud service with Terraform</title>
      <dc:creator>Erika Tan</dc:creator>
      <pubDate>Wed, 10 Aug 2022 18:03:13 +0000</pubDate>
      <link>https://dev.to/erikatstripe/deploy-a-stripe-sample-to-a-cloud-service-with-terraform-2l08</link>
      <guid>https://dev.to/erikatstripe/deploy-a-stripe-sample-to-a-cloud-service-with-terraform-2l08</guid>
      <description>&lt;p&gt;The &lt;a href="https://github.com/stripe-samples"&gt;Stripe samples&lt;/a&gt; are great ways to see how to build payments into your web app. This article will show you how to use &lt;a href="https://www.terraform.io/"&gt;Terraform&lt;/a&gt;, a popular infrastructure-as-code software tool, to deploy a Stripe sample to AWS. &lt;/p&gt;

&lt;p&gt;But why use Terraform? Terraform lets you provision API objects with specific attributes, such as EC2 instances, Lambda functions, etc. through a declarative configuration language. It’s also very flexible: it can simultaneously manage multiple different providers, as opposed to CloudFormation, which is only limited to AWS. You can even write your own Terraform provider for any API, and publish it to the open-sourced Terraform Registry!&lt;/p&gt;

&lt;h1&gt;
  
  
  Before you begin
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;You will need an AWS account with Identity and Access Management (&lt;a href="https://aws.amazon.com/iam/?trk=ced3f61b-256e-4936-a949-66a0aee40a02&amp;amp;sc_channel=ps&amp;amp;sc_campaign=acquisition&amp;amp;sc_medium=ACQ-P%7CPS-GO%7CNon-Brand%7CDesktop%7CSU%7CSecurity%20ID%20Compliance%7CSolution%7CUS%7CEN%7CDSA&amp;amp;ef_id=Cj0KCQjwz96WBhC8ARIsAATR250nztYiRCl29e8AEmTn1fDWeSkIreH-fQcBaiAoSDTD0ShXBfO7xyMaAjpGEALw_wcB:G:s&amp;amp;s_kwcid=AL!4422!3!579296534079!!!g!!"&gt;IAM&lt;/a&gt;) credentials. Specifically, you will need an AWS Access Key ID and Secret Access Key (refer to the “Programmatic access” section of the &lt;a href="https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html"&gt;AWS security credentials documentation&lt;/a&gt; for detailed instructions on how to retrieve them). &lt;/li&gt;
&lt;li&gt;You will also need a Stripe account.&lt;/li&gt;
&lt;li&gt;Follow the instructions to &lt;a href="https://learn.hashicorp.com/tutorials/terraform/install-cli"&gt;install Terraform&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Clone the &lt;a href="https://git.corp.stripe.com/stripe-sandbox/terraform-checkout-one-time-payments"&gt;terraform-checkout-one-time-payments&lt;/a&gt; repository to walk through the example steps. (This repository builds off of the original &lt;a href="https://github.com/stripe-samples/checkout-one-time-payments"&gt;checkout-one-time-payments&lt;/a&gt; Stripe sample repository.) You will have to follow the steps for running locally.

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;[Important] The steps in this tutorial assume that you are running the Stripe sample with HTML and a JavaScript (Node) server implementation. So when you clone and configure the sample, be sure to choose these options.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  High-level overview
&lt;/h1&gt;

&lt;p&gt;We will use Terraform to provision many necessary AWS resources to host the app, which have been defined in &lt;code&gt;main.tf&lt;/code&gt; in the terraform-checkout-one-time-payments repository. At a high level, we are creating a Lambda function that will handle API queries and connect to the AWS API Gateway service, which will also display the frontend of the app. Each of these resources is defined in its own &lt;code&gt;resource&lt;/code&gt; block, like so:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resource "aws_api_gateway_rest_api" "expressapi" {
    body = jsonencode({
        openapi = "3.0.1"
        info = {
            title   = "ExpressApi"
            version = "1.0"
        }
        paths = ...
    })
    binary_media_types = [ "*/*" ]
    name = "ExpressApi"
}

resource "aws_lambda_function" "expresslambdafunction" {
  ...
}

// ... other resources
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In addition to the new &lt;code&gt;main.tf&lt;/code&gt; file, the other major difference between the deployment tutorial repository and the original checkout-one-time-payments repository is the restructuring of the code directory tree. This is necessary for webpack to build and bundle the application. &lt;/p&gt;

&lt;h1&gt;
  
  
  Deploying with Terraform
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Step 1
&lt;/h2&gt;

&lt;p&gt;Run &lt;code&gt;npm build&lt;/code&gt; to initiate the build process and create the &lt;code&gt;dist&lt;/code&gt;  folder, which will come in handy in the next step.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2
&lt;/h2&gt;

&lt;p&gt;Now we need to prepare the code to be uploaded to the AWS Lambda function. Create one zip file containing all of the contents in the &lt;code&gt;dist&lt;/code&gt; directory, but &lt;em&gt;do not zip the&lt;/em&gt; &lt;code&gt;dist&lt;/code&gt; &lt;em&gt;directory itself&lt;/em&gt;. Name it whatever you’d like; running the Terraform script will later prompt you to input the name of the zip file you’d like to upload into Lambda. An example Linux command that you could run to do this would be:&lt;br&gt;
&lt;code&gt;cd dist &amp;amp;&amp;amp; zip -r ../out.zip .&lt;/code&gt;&lt;br&gt;
This will create a zip file titled &lt;code&gt;out.zip&lt;/code&gt; in the root folder.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3
&lt;/h2&gt;

&lt;p&gt;You are almost ready to initialize terraform! Before you continue, you will first have to use your IAM credentials to authenticate the Terraform AWS provider. Retrieve your AWS Access Key ID and Secret Access Key (refer to the “Programmatic access” section of the &lt;a href="https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html"&gt;AWS security credentials documentation&lt;/a&gt; for detailed instructions). Then, in a &lt;code&gt;.env&lt;/code&gt; file, correspondingly set the &lt;code&gt;AWS_ACCESS_KEY_ID&lt;/code&gt; and &lt;code&gt;AWS_SECRET_ACCESS_KEY&lt;/code&gt;.&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AWS_ACCESS_KEY_ID=&lt;br&gt;
AWS_SECRET_ACCESS_KEY=&lt;br&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Step 4&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;Now you can begin using Terraform commands!&lt;br&gt;
While you are in the root directory, run &lt;code&gt;terraform&lt;/code&gt; &lt;code&gt;init&lt;/code&gt;, which will download the necessary providers and initialize Terraform.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5
&lt;/h2&gt;

&lt;p&gt;Then, apply the AWS configurations using &lt;code&gt;terraform apply&lt;/code&gt;. This will create all of the needed AWS resources for deployment. &lt;/p&gt;

&lt;h2&gt;
  
  
  Step 6
&lt;/h2&gt;

&lt;p&gt;Enter the name of the zip file you created in the previous step when the Terraform CLI prompts you to do so. This will kick off the process of creating an API Gateway, Lambda function, and IAM roles. Once finished, it will output a few URLs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;lambda_function_console_url&lt;/code&gt;: The URL to the created Lambda function overview &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SqtegSRF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://paper-attachments.dropbox.com/s_152C22A11D34A75DD361ED76FD3D5D47D40E773F462A560BCAD3351290374F7F_1658178450764_Screen%2BShot%2B2022-07-18%2Bat%2B2.07.20%2BPM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SqtegSRF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://paper-attachments.dropbox.com/s_152C22A11D34A75DD361ED76FD3D5D47D40E773F462A560BCAD3351290374F7F_1658178450764_Screen%2BShot%2B2022-07-18%2Bat%2B2.07.20%2BPM.png" alt="" width="880" height="415"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;api_gateway_console_url&lt;/code&gt;: The API Gateway console, where you can view all created API Gateway REST APIs — the newly created one will be called “ExpressApi” if you didn’t rename it in &lt;code&gt;main.tf&lt;/code&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zkGco5qF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://paper-attachments.dropbox.com/s_152C22A11D34A75DD361ED76FD3D5D47D40E773F462A560BCAD3351290374F7F_1658178353433_Screen%2BShot%2B2022-07-18%2Bat%2B2.05.25%2BPM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zkGco5qF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://paper-attachments.dropbox.com/s_152C22A11D34A75DD361ED76FD3D5D47D40E773F462A560BCAD3351290374F7F_1658178353433_Screen%2BShot%2B2022-07-18%2Bat%2B2.05.25%2BPM.png" alt="" width="880" height="202"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;api_url&lt;/code&gt;: The API endpoint itself where you can try out the Stripe sample&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ef1UlaG0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://paper-attachments.dropbox.com/s_152C22A11D34A75DD361ED76FD3D5D47D40E773F462A560BCAD3351290374F7F_1658178400451_Screen%2BShot%2B2022-07-18%2Bat%2B2.06.29%2BPM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ef1UlaG0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://paper-attachments.dropbox.com/s_152C22A11D34A75DD361ED76FD3D5D47D40E773F462A560BCAD3351290374F7F_1658178400451_Screen%2BShot%2B2022-07-18%2Bat%2B2.06.29%2BPM.png" alt="" width="880" height="430"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 7
&lt;/h2&gt;

&lt;p&gt;At this point, you should be almost done, however, the &lt;code&gt;DOMAIN&lt;/code&gt; environment variable still needs to be set so that the app will know which URL to redirect to once finishing the payments flow. &lt;/p&gt;

&lt;p&gt;Unfortunately, the &lt;code&gt;DOMAIN&lt;/code&gt; cannot be known ahead of time before provisioning the API endpoint URL, and there is no way (yet) to declare using the output URL as the input to the Lambda function in Terraform. So, to get around this, paste the URL after the first call to  &lt;code&gt;terraform apply&lt;/code&gt;  as the &lt;code&gt;DOMAIN&lt;/code&gt; environment variable in &lt;code&gt;main.tf&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Navigate to &lt;code&gt;main.tf&lt;/code&gt;, copy the value of &lt;code&gt;api_url&lt;/code&gt; and paste it as the &lt;code&gt;DOMAIN&lt;/code&gt; environment variable under the &lt;code&gt;"aws_lambda_function" "expresslambdafunction"&lt;/code&gt; resource, then copy the rest of the environment variables from &lt;code&gt;server/.env&lt;/code&gt; . Run &lt;code&gt;terraform apply&lt;/code&gt; again to update with these changes.&lt;/p&gt;

&lt;p&gt;The app should now be ready! Visit the &lt;code&gt;api_url&lt;/code&gt; to view the running app.&lt;/p&gt;

&lt;h1&gt;
  
  
  Wrap up
&lt;/h1&gt;

&lt;p&gt;In this article, you learned how to deploy an existing Stripe sample application to AWS by using the checkout-one-time-payments sample as a walkthrough. You also initialized (&lt;code&gt;terraform init&lt;/code&gt;) and ran (&lt;code&gt;terraform apply&lt;/code&gt; ) a Terraform script (&lt;code&gt;main.tf&lt;/code&gt;) to automatically provision the resources needed for the deployment. &lt;/p&gt;

&lt;p&gt;This tutorial just scratches the surface of what you can do with Terraform. For further exploration, consider these resources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://learn.hashicorp.com/terraform"&gt;Official Terraform Tutorials&lt;/a&gt;: learn about CLI commands, the Terraform Configuration Language syntax, writing modules, provisioning infrastructure, and Terraform state&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://registry.terraform.io/"&gt;Terraform Registry&lt;/a&gt;: browse the different resources types that existing providers can provision for you today&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An official Terraform provider for the Stripe API is also currently in the works. We hope to publish it to the Terraform Registry soon. This will allow you to define and manage Stripe objects in your own Terraform files. A great use case for this would be creating and maintaining your webhook endpoints. &lt;/p&gt;

&lt;p&gt;We’d also love to hear what you think of this tutorial and if there are other interesting ways that you use Terraform!&lt;/p&gt;

&lt;h1&gt;
  
  
  About the author
&lt;/h1&gt;

&lt;p&gt;Erika Tan is an intern at Stripe on the Sandboxes team. Her favorite hobbies outside of work are reading and trying new restaurants.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
