{"id":15596,"date":"2022-02-23T02:34:51","date_gmt":"2022-02-23T07:34:51","guid":{"rendered":"https:\/\/qxf2.com\/blog\/?p=15596"},"modified":"2024-03-14T09:20:59","modified_gmt":"2024-03-14T13:20:59","slug":"ci-cd-localstack","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/ci-cd-localstack\/","title":{"rendered":"Set up a CI\/CD pipeline to run lambda tests using AWS cloud service emulator named LocalStack"},"content":{"rendered":"<p><span style=\"font-weight: 400;\"> Recently, I have explored <a href=\"https:\/\/github.com\/localstack\/localstack\">LocalStack<\/a> AWS local stack to test cloud application locally. You can test the lambda in the AWS lambda console or Moto i.e. mocking. However running tests against LocalStack, would be good as this would not incur any cost of using AWS Services. LocalStack can be used for running any AWS service locally as well as on the CI\/CD environment as well.<\/p>\n<p>I was looking to develop CI\/CD flow for tests, I could not locate much samples on the internet. In this blog, I have recorded the flow I have written running ci-cd against LocalStack.<\/p>\n<p><\/span><\/p>\n<hr \/>\n<h3><b>Steps to write ci-cd flow:<\/b><\/h3>\n<p>This can be achieved using following steps:<\/p>\n<p>1. First start localstack services, this can be done using following step, you can mention required services under SERVICES mentioned in env<\/p>\n<pre lang=\"Python\">\r\njobs:\r\n  localstack:\r\n    runs-on: ubuntu-latest\r\n    services:\r\n      localstack:\r\n        image: localstack\/localstack:latest\r\n        env:\r\n          SERVICES: lambda\r\n          DEFAULT_REGION: eu-west-1\r\n          AWS_ACCESS_KEY_ID: localkey\r\n          AWS_SECRET_ACCESS_KEY: localsecret\r\n        ports:\r\n          - 4566:4566\r\n          - 4571:4571\r\n<\/pre>\n<p>2. Install python dependencies and run tests<\/p>\n<pre lang=\"Python\">\r\nsteps:\r\n      - uses: actions\/checkout@v2\r\n      - name: Install Python 3.8\r\n        uses: actions\/setup-python@v1\r\n        with:\r\n          python-version: 3.8\r\n      - name: Install dependencies\r\n        run: |\r\n          pip3 install --upgrade pip==20.0.1\r\n          pip3 install -r requirements.txt\r\n      # Execute Tests lambda\r\n      - name: Run test for sample lambda\r\n        run: |\r\n          cd lambda\r\n          pytest -sv\r\n<\/pre>\n<p>Note: I have used one sample lambda as an application under test. The test case written to test the sample code as well as ci-cd yml file. I have kept all these in the <a href=\"https:\/\/github.com\/rahul-bhave\/localstack-utilities\">Repo<\/a><\/p>\n<hr \/>\n<h3><b>ci-cd workflow file after including all above steps:<\/b><\/h3>\n<pre lang=\"Python\">\r\n\r\n\"\"\"\r\nAWS CI-CD Flow using LocalStack\r\n\"\"\"\r\nname: Aws Localstack CI\r\non: push\r\njobs:\r\n  localstack:\r\n    runs-on: ubuntu-latest\r\n    services:\r\n      localstack:\r\n        image: localstack\/localstack:latest\r\n        env:\r\n          SERVICES: lambda\r\n          DEFAULT_REGION: eu-west-1\r\n          AWS_ACCESS_KEY_ID: localkey\r\n          AWS_SECRET_ACCESS_KEY: localsecret\r\n        ports:\r\n          - 4566:4566\r\n          - 4571:4571\r\n    steps:\r\n      - uses: actions\/checkout@v2\r\n\r\n      - name: Install Python 3.8\r\n        uses: actions\/setup-python@v1\r\n        with:\r\n          python-version: 3.8\r\n      - name: Install dependencies\r\n        run: |\r\n          pip3 install --upgrade pip==20.0.1\r\n          pip3 install -r requirements.txt\r\n      # Execute Tests lambda\r\n      - name: Run test for sample lambda\r\n        run: |\r\n          pytest -sv\r\n  \r\n<\/pre>\n<p>After running test following output will be shown:<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2021\/11\/AWS-LocalStack-CI-CD.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2021\/11\/AWS-LocalStack-CI-CD.png\" alt=\"\"><\/a><\/a><\/p>\n<hr\/>\n<p>I hope this blog helped you to get acquainted and use this library quickly. Thanks for reading!<\/p>\n<hr\/>\n","protected":false},"excerpt":{"rendered":"<p>Recently, I have explored LocalStack AWS local stack to test cloud application locally. You can test the lambda in the AWS lambda console or Moto i.e. mocking. However running tests against LocalStack, would be good as this would not incur any cost of using AWS Services. LocalStack can be used for running any AWS service locally as well as on [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52,301,316,297],"tags":[],"class_list":["post-15596","post","type-post","status-publish","format-standard","hentry","category-continuous-integration","category-github-action","category-infrastructure","category-localstack"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/15596","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\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=15596"}],"version-history":[{"count":11,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/15596\/revisions"}],"predecessor-version":[{"id":16035,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/15596\/revisions\/16035"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=15596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=15596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=15596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}