{"id":18573139,"url":"https://github.com/localstack-samples/sample-chaos-api-serverless","last_synced_at":"2025-05-15T23:12:51.302Z","repository":{"id":232740881,"uuid":"785076335","full_name":"localstack-samples/sample-chaos-api-serverless","owner":"localstack-samples","description":null,"archived":false,"fork":false,"pushed_at":"2024-08-26T09:26:24.000Z","size":21506,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-12-26T14:28:56.515Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/localstack-samples.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-04-11T06:32:42.000Z","updated_at":"2024-08-26T09:26:27.000Z","dependencies_parsed_at":"2024-07-26T10:12:18.763Z","dependency_job_id":"682d1cf9-b7ba-4616-b7dd-626cb23078e2","html_url":"https://github.com/localstack-samples/sample-chaos-api-serverless","commit_stats":null,"previous_names":["localstack-samples/sample-outages-extension-serverless","localstack-samples/sample-chaos-api-serverless"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/localstack-samples%2Fsample-chaos-api-serverless","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/localstack-samples%2Fsample-chaos-api-serverless/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/localstack-samples%2Fsample-chaos-api-serverless/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/localstack-samples%2Fsample-chaos-api-serverless/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/localstack-samples","download_url":"https://codeload.github.com/localstack-samples/sample-chaos-api-serverless/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239309239,"owners_count":19617848,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T23:08:08.033Z","updated_at":"2025-02-17T14:40:50.449Z","avatar_url":"https://github.com/localstack-samples.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Simulating outages for local cloud apps with LocalStack's Chaos API\n\nLocalStack's [Chaos API](https://docs.localstack.cloud/user-guide/chaos-engineering/chaos-api/) enables you to simulate a local outage, right on your developer machine. In this demo, we set up an HTTP CRUD API functioning as a Product Management System, and use the Chaos API to simulate an outage in the DynamoDB table. We'll further use `pytest` to test the application's behavior during the outage.\n\n## Architecture\n\n![Architecture](images/architecture.png)\n\nThe architecture deploys:\n\n-   A DynamoDB table named  `Products`.\n-   Three Lambda functions:\n    -   `add-product`  for product addition.\n    -   `get-product`  for retrieving a product.\n    -   `process-product-events`  for event processing and DynamoDB writes.\n-   A locally hosted REST API named  `quote-api-gateway`.\n-   SNS topic named  `ProductEventsTopic`  and SQS queue named  `ProductEventsQueue`.\n-   API Gateway resource named  `productApi`  with additional  `GET`  and  `POST`  methods.\n\nAdditionally, the applications sets up a subscription between the SQS queue and SNS topic, along with an event source mapping between the SQS queue and the `process-product-events` Lambda function.\n\n## Prerequisites\n\n-   [LocalStack Docker image](https://docs.localstack.cloud/references/docker-images/#localstack-pro-image)  \u0026  [`LOCALSTACK_AUTH_TOKEN`](https://docs.localstack.cloud/getting-started/auth-token/)\n-   [Docker Compose](https://docs.docker.com/compose/install/)\n-   [AWS CLI](https://docs.aws.amazon.com/cli/v1/userguide/cli-chap-install.html)  \u0026  [`awslocal`  wrapper](https://docs.localstack.cloud/user-guide/integrations/aws-cli/#localstack-aws-cli-awslocal)\n-   [Maven 3.8.5](https://maven.apache.org/install.html)  \u0026  [Java 17](https://www.java.com/en/download/help/download_options.html)\n-   [Python](https://www.python.org/downloads/)  \u0026  [`pytest`  framework](https://docs.pytest.org/en/8.0.x/)\n-   [`cURL`](https://curl.se/docs/install.html)\n\n## Instructions\n\n### Start your LocalStack container\n\nStart your LocalStack container using the Docker Compose configuration:\n\n```bash\nexport LOCALSTACK_AUTH_TOKEN=your_auth_token\ndocker-compose up\n```\n\nThe Docker Compose configuration will:\n\n* Start the LocalStack container.\n* Create your cloud infrastructure.\n\n### Test the infrastructure\n\nAfter a successful deployment, test the infrastructure by running the following commands:\n\n```bash\ncurl --location 'http://12345.execute-api.localhost.localstack.cloud:4566/dev/productApi' \\\n--header 'Content-Type: application/json' \\\n--data '{\n  \"id\": \"prod-2004\",\n  \"name\": \"Ultimate Gadget\",\n  \"price\": \"49.99\",\n  \"description\": \"The Ultimate Gadget is the perfect tool for tech enthusiasts looking for the next level in gadgetry. Compact, powerful, and loaded with features.\"\n}'\n```\n\nThe output will be:\n\n```bash\nProduct added/updated successfully.\n```\n\nYou can navigate to the [DynamoDB Resource Browser](https://app.localstack.cloud/inst/default/resources/dynamodb) to view the added product.\n\n### Simulate an outage\n\nYou can simulate an outage in your local DynamoDB table by running the following command:\n\n```bash\ncurl --location --request POST 'http://localhost.localstack.cloud:4566/_localstack/chaos/faults' \\\n  --header 'Content-Type: application/json' \\\n  --data '\n  [\n    {\n      \"service\": \"dynamodb\",\n      \"region\": \"us-east-1\"\n    }\n  ]'\n```\n\nThe output will be:\n\n```bash\n[{\"service\": \"dynamodb\", \"region\": \"us-east-1\"}]\n```\n\n### Implement error handling\n\nThe application is designed to handle the outage by sending a message to the SQS queue using a Lambda function. Run the following command to create the necessary resources:\n\n```bash\n./setup-solution.sh\n```\n\nYou can now test the infrastructure again by running the following command:\n\n```bash\ncurl --location 'http://12345.execute-api.localhost.localstack.cloud:4566/dev/productApi' \\\n--header 'Content-Type: application/json' \\\n--data '{\n  \"id\": \"prod-1003\",\n  \"name\": \"Super Widget\",\n  \"price\": \"29.99\",\n  \"description\": \"A versatile widget that can be used for a variety of purposes. Durable, reliable, and affordable.\"\n}'\n```\n\nThe following error will be displayed:\n\n```bash\nA DynamoDB error occurred. Message sent to queue.\n```\n\n### Stop the outage\n\nTo stop the outage, run the following command:\n\n```bash\ncurl --location --request POST 'http://localhost.localstack.cloud:4566/_localstack/chaos/faults' \\\n  --header 'Content-Type: application/json' \\\n  --data '[]'\n```\n\nYou can now see that the product has been added successfully by running the following command:\n\n```bash\nawslocal dynamodb scan --table-name Products\n```\n\nThe following output will be displayed:\n\n```bash\n{\n    \"Items\": [\n        {\n            \"name\": {\n                \"S\": \"Super Widget\"\n            },\n            ...\n        },\n        {\n            \"name\": {\n                \"S\": \"Ultimate Gadget\"\n            },\n            ...\n        }\n    ],\n    \"Count\": 2,\n    \"ScannedCount\": 2,\n    \"ConsumedCapacity\": null\n}\n```\n\n### Running an integration test\n\nTo run an integration test, run the following command:\n\n```bash\npytest\n```\n\nThis command will test the application's behavior during an outage, and give you an instant validation that your architecture is resilient to outages.\n\n## License\n\nThis code is licensed under the Apache 2.0 License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flocalstack-samples%2Fsample-chaos-api-serverless","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flocalstack-samples%2Fsample-chaos-api-serverless","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flocalstack-samples%2Fsample-chaos-api-serverless/lists"}