{"id":19046,"date":"2023-08-11T08:29:23","date_gmt":"2023-08-11T12:29:23","guid":{"rendered":"https:\/\/qxf2.com\/blog\/?p=19046"},"modified":"2024-03-14T09:20:39","modified_gmt":"2024-03-14T13:20:39","slug":"test-lambda-deployment-ci-localstack","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/test-lambda-deployment-ci-localstack\/","title":{"rendered":"Test Lambda before deployment on CI using LocalStack"},"content":{"rendered":"<p>In my previous posts, I covered <a href=\"https:\/\/qxf2.com\/blog\/testing-aws-lambda-locally-using-localstack-and-pytest\/\" rel=\"noopener\" target=\"_blank\">how to write tests for Lambdas, run them on LocalStack<\/a> and addressed the <a href=\"https:\/\/qxf2.com\/blog\/localstack-security-token-included-invalid\/\" rel=\"noopener\" target=\"_blank\">issue<\/a> I encountered while working on <a href=\"https:\/\/localstack.cloud\/\" rel=\"noopener\" target=\"_blank\">LocalStack<\/a>. Now, in this post, I will show you how to set up a GitHub Action job to run tests on LocalStack before deploying Lambdas to production. At <a href=\"https:\/\/qxf2.com\/contact?utm_source=test_lambda_before_deployment_on_ci&#038;utm_medium=click&#038;utm_campaign=From%20blog\" rel=\"noopener\" target=\"_blank\">Qxf2<\/a>, we place high importance on CI\/CD, and it is crucial for us to test Lambdas before deploying them into production. LocalStack proves to be helpful by allowing us to deploy Lambdas locally for testing purposes.<\/p>\n<figure id=\"attachment_19462\" aria-describedby=\"caption-attachment-19462\" style=\"width: 805px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/08\/localstack-ci-workflow_new3.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/08\/localstack-ci-workflow_new3.png\" alt=\"CICD Workflow\" width=\"805\" height=\"233\" class=\"size-full wp-image-19462\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/08\/localstack-ci-workflow_new3.png 805w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/08\/localstack-ci-workflow_new3-300x87.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/08\/localstack-ci-workflow_new3-768x222.png 768w\" sizes=\"auto, (max-width: 805px) 100vw, 805px\" \/><\/a><figcaption id=\"caption-attachment-19462\" class=\"wp-caption-text\">CICD Workflow<\/figcaption><\/figure>\n<p>Nowadays, we are using Github Action for CI\/CD. Look at the steps below for setup.<br \/>\n<strong>Steps to setup with Github Action:<\/strong><br \/>\n1. Create a .yml file under .github\/workflow directory<br \/>\n2. Write a workflow to run Lambda tests on LocalStack and then deploy to production<br \/>\n3. Use act tool to test your workflow locally<br \/>\n4. Push your code and merge it into master\/main branch<\/p>\n<hr>\n<h4>1. Create a .yml file under .github\/workflow directory:<\/h4>\n<p>Please skip this step if you already have a YAML file for GitHub Actions. If you haven&#8217;t set up GitHub Actions yet, please create a directory named .github\/workflows and create a file named url-filter-lambda.yml<\/p>\n<hr>\n<h4>2. Write a workflow to run Lambda tests on LocalStack and then deploy to production:<\/h4>\n<p>Now we need to write a workflow to start LocalStack and run the test that deploys the Lambda and tests it.  Please refer our <a href=\"https:\/\/qxf2.com\/blog\/testing-aws-lambda-locally-using-localstack-and-pytest\/\" rel=\"noopener\" target=\"_blank\">previous blog<\/a> for the test and docker-compose file used to start LocalStack. Look at the workflow below:<\/p>\n<pre lang='python'> \r\nname: Test with LocalStack\r\n\r\non:\r\n  push:\r\n    branches:\r\n      - main # Adjust the branch name if needed\r\n\r\njobs:\r\n  test:\r\n    name: Test\r\n    runs-on: ubuntu-latest\r\n\r\n    steps:\r\n      - name: Checkout repository\r\n        uses: actions\/checkout@v2\r\n\r\n      - name: Set up Docker Compose\r\n        run: |\r\n          sudo apt-get update\r\n          sudo apt-get install -y docker-compose\r\n\r\n      - name: Start LocalStack\r\n        run: |\r\n          docker-compose -f docker-compose.yml up -d\r\n\r\n      - name: Wait for LocalStack to be ready\r\n        run: |\r\n          timeout 300 bash -c 'until echo > \/dev\/tcp\/localhost\/4566; do sleep 1; done'\r\n\r\n      - name: Set up Python\r\n        uses: actions\/setup-python@v2\r\n        with:\r\n          python-version: \"3.8\" # Replace version with your desired Python version \r\n\r\n      - name: Install dependencies\r\n        run: pip install -r requirements.txt \r\n\r\n      - name: Run tests\r\n        run: pytest -s -v test_lambda.py # Replace with the appropriate command to run your tests\r\n\r\n      - name: Stop LocalStack\r\n        run: docker-compose -f docker-compose.yml down\r\n\r\n  production:\r\n    name: Deploy to production\r\n    needs: test\r\n    runs-on: ubuntu-latest\r\n\r\n    steps:\r\n    .......\r\n    .......\r\n    .......\r\n<\/pre>\n<p>Refer to our GitHub Action script of the URL filter Lambda for your reference <a href=\"https:\/\/github.com\/qxf2\/qxf2-lambdas\/blob\/master\/.github\/workflows\/url_filtering_lambda_rohini.yml\" rel=\"noopener\" target=\"_blank\">here<\/a>. Our workflow tests the Lambda on LocalStack before deploying it to the Staging and the Production.<\/p>\n<hr>\n<h4>3. Use act tool to test your workflow locally: <\/h4>\n<p>To test a workflow locally before pushing the changes, we need to use &#8216;act&#8217; tool. Refer to the <a href=\"https:\/\/github.com\/nektos\/act\" rel=\"noopener\" target=\"_blank\">nektos\/act<\/a> README and install the &#8216;act&#8217; tool. Once you&#8217;re set with the &#8216;act&#8217; tool, run the script using the &#8216;act&#8217; command.<\/p>\n<p>Command to run the workflow:<\/p>\n<pre lang=\"python\"> act -W <Workflow yml file path --env-file env_staging.env<\/pre>\n<p>In the above command, you can use the --env-file option if you want to pass environment variable values, or you can skip it. For more commands, please refer to the nektos\/act readme file.<\/p>\n<hr>\n<h4>4. Push your code and merge it into master\/main branch:<\/h4>\n<p>Once you can successfully run the workflow using 'act,' you can push your changes and merge them with the master\/main branch. After the merge, GitHub Actions trigger the workflow for every push to the branch mentioned under the workflow. It runs the test on LocalStack, and if the test runs successfully, it proceeds to the next jobs. <\/p>\n<hr>\n<p>I hope this blog helps you get set up with GitHub Actions for your Lambda deployment.<\/p>\n<hr>\n<h4>Hire testers from Qxf2!<\/h4>\n<p><a href=\"https:\/\/qxf2.com\/contact?utm_source=test_lambda_before_deployment_on_ci&#038;utm_medium=click&#038;utm_campaign=From%20blog\">Hiring Qxf2<\/a> brings technical expertise that extends beyond traditional test automation. Our skilled testers possess diverse technical skills and practical knowledge. We excel in crafting and executing comprehensive test strategies, enabling seamless integration of testing into the development process. Our team's proficiency in various tools and frameworks ensures efficient testing of complex software systems, contributing to enhanced product quality and faster iteration cycles.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>In my previous posts, I covered how to write tests for Lambdas, run them on LocalStack and addressed the issue I encountered while working on LocalStack. Now, in this post, I will show you how to set up a GitHub Action job to run tests on LocalStack before deploying Lambdas to production. At Qxf2, we place high importance on CI\/CD, [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[301,316,179,297],"tags":[],"class_list":["post-19046","post","type-post","status-publish","format-standard","hentry","category-github-action","category-infrastructure","category-lambda-functions","category-localstack"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/19046","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=19046"}],"version-history":[{"count":17,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/19046\/revisions"}],"predecessor-version":[{"id":20308,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/19046\/revisions\/20308"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=19046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=19046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=19046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}