<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by ChengHsun Yang on Medium]]></title>
        <description><![CDATA[Stories by ChengHsun Yang on Medium]]></description>
        <link>https://medium.com/@chenghsunyang?source=rss-2fefbc615c2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Z66kjKmYRQ_FI5bU.jpg</url>
            <title>Stories by ChengHsun Yang on Medium</title>
            <link>https://medium.com/@chenghsunyang?source=rss-2fefbc615c2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 17:57:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@chenghsunyang/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Deploy a Flask app with AWS API Gateway managed via Zappa and Terraform]]></title>
            <link>https://medium.com/tech-blog-door2door/deploy-a-flask-app-with-aws-api-gateway-managed-via-zappa-and-terraform-91d3603721c?source=rss-2fefbc615c2------2</link>
            <guid isPermaLink="false">https://medium.com/p/91d3603721c</guid>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[flask]]></category>
            <dc:creator><![CDATA[ChengHsun Yang]]></dc:creator>
            <pubDate>Wed, 18 Apr 2018 09:05:02 GMT</pubDate>
            <atom:updated>2018-04-18T09:22:53.532Z</atom:updated>
            <content:encoded><![CDATA[<p>Zappa and Terraform can work happily together, so should we.</p><h3>Some context</h3><p>The ability to enable fast iterations without worrying too much about managing infrastructure has made serverless architecture very popular. For <a href="https://www.door2door.io/">door2door</a> <a href="https://www.door2door.io/en/insights.html">Insights</a>, we use <a href="http://flask.pocoo.org/">Flask</a> as the web framework of choice. It’s light-weight and flexible nature has made it perfect for our application.</p><p>To deploy a Flask application to <a href="https://aws.amazon.com/lambda/">AWS Lambda</a> involve zipping all the dependencies and upload the zip file to an AWS Lambda instance. This is sometimes not a trivial task since moving the <a href="https://pypi.python.org/pypi/pip">pip</a> installed site packages and have them in places where AWS Lambda Python runtime can find them correctly can be cumbersome. This is why tools like<a href="https://github.com/Miserlou/Zappa"> Zappa</a> were created.</p><p>Quoting <a href="https://github.com/Miserlou/Zappa/blob/master/README.md">Zappa’s readme</a>:</p><blockquote>Zappa makes it super easy to build and deploy server-less, event-driven Python applications (including, but not limited to, WSGI web apps) on AWS Lambda + API Gateway.</blockquote><p>This is wonderful. Zappa handles the tedious work of configuring Route53, API Gateway proxy and keep-warm events. It even offers a super nice command to inspect logs for individual environment (zappa tail if you’re curious). It really is a Swiss knife for Python Lambda applications.</p><h3>Problem</h3><p><a href="https://en.wikipedia.org/wiki/Infrastructure_as_Code">At door2door, almost everything is infrastructure as code (IaC)</a>. We ensure our infrastructure’s correctness, security and deployment speed by using <a href="https://www.terraform.io/">Terraform</a> as our IaC tool. However, combining Terraform with Zappa creates some problems.</p><p>Zappa creates it’s own AWS resources while deploying applications. These resources created are never tracked by Terraform. This causes problems when we try to deploy or update the infrastructure.</p><h3>Solution</h3><p>The solution is to hand over the creation of these resources to Terraform and configure Zappa to use some of the created resources while give up managing others. The resources are:</p><ol><li>an S3 bucket to hold the zip file</li><li>a configured Lambda function</li><li>a configured API Gateway instance</li><li>a CloudWatch scheduled event for keeping service warm</li><li>VPC configurations</li><li>a certificate deployment for Route53 DNS Authentication</li></ol><p>Here’s roughly the Terraform code for creating them, notice this is omitting the policies, IAM roles, VPC setup, Cert and the actual values:</p><pre># S3 bucket<br>resource &quot;aws_s3_bucket&quot; &quot;bucket&quot; {<br>  count         = <br>  bucket        = &quot;mybucket&quot;<br>  acl           = <br>  force_destroy =<br>  tags          =<br>}</pre><pre># Lambda function<br>resource &quot;aws_lambda_function&quot; &quot;function&quot; {<br>  function_name = &quot;myapp&quot;<br>  description   = <br>  role          = <br>  handler       =<br>  runtime       = <br>  vpc_config {<br>    subnet_ids         = <br>    security_group_ids = <br>  }<br>}</pre><pre># API Gateway<br>resource &quot;aws_api_gateway_rest_api&quot; &quot;myapp-apigw&quot; {<br>  name        = <br>  description = <br>  binary_media_types = [&quot;*/*&quot;]<br>}</pre><pre>resource &quot;aws_lambda_permission&quot; &quot;apigw&quot; {<br>  statement_id  = &quot;AllowExecutionFromAPIGateway&quot;<br>  action        = &quot;lambda:InvokeFunction&quot;<br>  function_name = &quot;&lt;lambda arn&gt;&quot;<br>  principal     = &quot;apigateway.amazonaws.com&quot;<br>  source_arn    = <br>}</pre><pre>resource &quot;aws_api_gateway_resource&quot; &quot;mock&quot; {<br>  rest_api_id = <br>  parent_id   = <br>  path_part   = <br>}</pre><pre>resource &quot;aws_api_gateway_method&quot; &quot;mock&quot; {<br>  rest_api_id   = <br>  resource_id   = <br>  http_method   = <br>  authorization = <br>}</pre><pre>resource &quot;aws_api_gateway_integration&quot; &quot;mock&quot; {<br>  rest_api_id = <br>  resource_id = <br>  http_method = <br>  type        =<br>}</pre><pre>resource &quot;aws_api_gateway_deployment&quot; &quot;stage&quot; {<br>  rest_api_id = <br>  stage_name  =<br>  depends_on  = <br>}</pre><pre>resource &quot;aws_api_gateway_domain_name&quot; &quot;endpoint&quot; {<br>  domain_name     = <br>  certificate_arn = <br>}</pre><pre>resource &quot;aws_api_gateway_base_path_mapping&quot; &quot;endpoint&quot; {<br>  api_id      = <br>  stage_name  = <br>  domain_name = <br>}</pre><pre>data &quot;aws_route53_zone&quot; &quot;dns_zone&quot; {<br>  name  = <br>}</pre><pre># CloudWatch Scheduled event<br>resource &quot;aws_cloudwatch_event_target&quot; &quot;scheduled_event_target&quot; {<br>  rule      = <br>  arn       = <br>  input_transformer = {<br>    input_paths =<br>    input_template =<br>  }<br>}</pre><pre>resource &quot;aws_cloudwatch_event_rule&quot; &quot;scheduled_event&quot; {<br>  name        =<br>  description = &quot;Scheduled event&quot;<br>  schedule_expression =<br>}</pre><pre>resource &quot;aws_lambda_permission&quot; &quot;keep_warm_event&quot; {<br>  statement_id  = &quot;AllowExecutionFromCloudWatch&quot;<br>  action        = &quot;lambda:InvokeFunction&quot;<br>  function_name = <br>  principal     = &quot;events.amazonaws.com&quot;<br>  source_arn    = <br>}</pre><p>Notice the <em>mock </em>resource created in API Gateway? This is needed for Terraform to create a API Gateway deploy. It is just a dummy resource.</p><p>The resources can be referenced by their <a href="https://www.terraform.io/intro/getting-started/outputs.html">output</a>’s arn. For example, the function_name for API Gateway’s permission can be the arn from the Lambda function, thus allows the API Gateway instance to invoke Lambda function.</p><p>Also the binary media type setup in API Gateway is a common pitfall while integrating Zappa. Zappa by default would base64 encode binary response, having this configured in API Gateway allows it to be correctly decoded by the client with correct headers.</p><p>After figuring out the Terraform managed environment, we’re almost done. Now we just need to tell Zappa to use our environment while give up controlling somethings:</p><ol><li>API Gateway integration</li><li>Keep warm event</li><li>Manage IAM roles creation</li></ol><p>This can be easily done by having a zappa_settings.json like:</p><pre>{<br>  &quot;dev&quot;: {<br>    &quot;app_function&quot;: &quot;myapp.app&quot;,<br>    &quot;lambda_description&quot;: &quot;Lambda function myapp&quot;,<br>    &quot;keep_warm&quot;: false,<br>    &quot;binary_support&quot;: true,<br>    &quot;apigateway_enabled&quot;: false,<br>    &quot;s3_bucket&quot;: &quot;mybucket&quot;,<br>    &quot;project_name&quot;: &quot;myapp&quot;,<br>    &quot;manage_roles&quot;: false,<br>    &quot;role_name&quot;: &quot;some_role_with_proper_s3_and_lambda_power&quot;,<br>    &quot;log_level&quot;: &quot;INFO&quot;,<br>  }<br>}</pre><p>A simple zappa deploy would deploy the app to the environment.</p><p>But we are still missing the resource creations our endpoints in API Gateway you say? We do this by having a swagger file at the end of our CI/CD pipeline and update the created API Gateway instance. This part will be covered in future blogposts.</p><h3>Conclusion</h3><p>It might seem to be too much trouble just to deploy a Python AWS Lambda application. But comparing to the usual <em>zip and upload</em>, this keeps the benefit of using all the Flask extensions and the useful Zappa utilities like zappa tail. Also IaC remains intact since Terraform is aware of the changes, this is very useful while working in an organisation setup. One potential improvement is to only use Zappa for packaging the application into a zip file and have the upload handled by some custom CI/CD function, this would save us from disabling things in zappa settings. But it might be just a matter of taste.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=91d3603721c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/tech-blog-door2door/deploy-a-flask-app-with-aws-api-gateway-managed-via-zappa-and-terraform-91d3603721c">Deploy a Flask app with AWS API Gateway managed via Zappa and Terraform</a> was originally published in <a href="https://medium.com/tech-blog-door2door">door2door Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>