{"id":25636496,"url":"https://github.com/santoshshinde2012/aws-cdk-python-psycopg2","last_synced_at":"2025-02-23T00:48:01.661Z","repository":{"id":274455337,"uuid":"912854848","full_name":"santoshshinde2012/aws-cdk-python-psycopg2","owner":"santoshshinde2012","description":"Getting Started Guide: How to Use psycopg2 and SQLAlchemy with AWS CDK (Python) and Test with SAM CLI","archived":false,"fork":false,"pushed_at":"2025-01-06T16:13:06.000Z","size":10,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-22T01:16:25.181Z","etag":null,"topics":["aws-cdk","aws-lambda","postgresql","psycopg2","psycopg2-binary","python","sqlalchemy"],"latest_commit_sha":null,"homepage":"https://medium.com/gitconnected/design-a-serverless-data-pipeline-daily-data-collection-transformation-and-storage-with-aws-b0910179e4e1","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/santoshshinde2012.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2025-01-06T14:31:34.000Z","updated_at":"2025-01-15T05:07:49.000Z","dependencies_parsed_at":"2025-01-27T13:02:54.949Z","dependency_job_id":null,"html_url":"https://github.com/santoshshinde2012/aws-cdk-python-psycopg2","commit_stats":null,"previous_names":["santoshshinde2012/aws-cdk-python-psycopg2"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/santoshshinde2012%2Faws-cdk-python-psycopg2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/santoshshinde2012%2Faws-cdk-python-psycopg2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/santoshshinde2012%2Faws-cdk-python-psycopg2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/santoshshinde2012%2Faws-cdk-python-psycopg2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/santoshshinde2012","download_url":"https://codeload.github.com/santoshshinde2012/aws-cdk-python-psycopg2/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240254163,"owners_count":19772386,"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":["aws-cdk","aws-lambda","postgresql","psycopg2","psycopg2-binary","python","sqlalchemy"],"created_at":"2025-02-23T00:48:00.989Z","updated_at":"2025-02-23T00:48:01.651Z","avatar_url":"https://github.com/santoshshinde2012.png","language":"Python","readme":"# Welcome to your CDK Python project!\n\nThe `cdk.json` file tells the CDK Toolkit how to execute your app.\n\nThis project is set up like a standard Python project. The initialization\nprocess also creates a virtualenv within this project, stored under the `.venv`\ndirectory. To create the virtualenv it assumes that there is a `python3`\n(or `python` for Windows) executable in your path with access to the `venv`\npackage. If for any reason the automatic creation of the virtualenv fails,\nyou can create the virtualenv manually.\n\nTo manually create a virtualenv on MacOS and Linux:\n\n```\n$ python3 -m venv .venv\n```\n\nAfter the init process completes and the virtualenv is created, you can use the following\nstep to activate your virtualenv.\n\n```\n$ source .venv/bin/activate\n```\n\nIf you are a Windows platform, you would activate the virtualenv like this:\n\n```\n% .venv\\Scripts\\activate.bat\n```\n\nOnce the virtualenv is activated, you can install the required dependencies.\n\n```\n$ pip install -r requirements.txt\n```\n\nIf we need a local PostgreSQL database, then use the below command to start PostgreSQL locally.\n\n`docker compose up -d`\n\nSome Python modules cannot be located by the lambda function. We need to create a zip of those modules and use them as a layer.\n\nFor that use below Docker command that is used to install Python dependencies in a way that is compatible with AWS Lambda.\n\n- `docker run -v \"$PWD\":/var/task \"amazon/aws-sam-cli-build-image-python3.9\" /bin/sh -c \"pip install -r lambda/requirements.txt -t python/lib/python3.9/site-packages/; exit\"`\n\nThen we need to create zip \n\n- `zip -r layers/pythonlib_layer.zip python`\n  \n\nAt this point you can now synthesize the CloudFormation template for this code.\n\n```\n$ cdk synth\n```\n\nTo add additional dependencies, for example other CDK libraries, just add\nthem to your `setup.py` file and rerun the `pip install -r requirements.txt`\ncommand.\n\n## Testing\n\n- `cdk synth \u003e template.yaml`\n- `sam build`\n- `sam local invoke MyLambdaFunction --no-event -t ./cdk.out/AwsCdkPythonPsycopg2Stack.template.json`\n- Read : `sam local invoke MyLambdaFunction --event events/read.json -t ./cdk.out/AwsCdkPythonPsycopg2Stack.template.json`\n- Write : `sam local invoke MyLambdaFunction --event events/write.json -t ./cdk.out/AwsCdkPythonPsycopg2Stack.template.json`\n\n### Payload\n\n- Write\n\n  ```\n  {\n      \"action\": \"write\",\n      \"data\": {\n          \"name\": \"Sample Record\",\n          \"description\": \"This is a test description.\"\n      }\n  }\n  ```\n\n- Read\n\n  ```\n  {\n      \"action\": \"read\"\n  }\n  ```\n\n### Result\n\n- Write\n\n  ```\n  (base) ➜  cdk-sam-demo git:(main) ✗ sam local invoke MyLambdaFunction --event events/write.json -t ./cdk.out/CdkSamDemoStack.template.json\n  Invoking app.lambda_handler (python3.9)\n  Decompressing\n  /Users/santosh/Documents/workspace/aws/cdk-sam-demo/cdk.out/asset.b9e5b43dd3a267\n  962ccec789725e1117a07177602f17375f57b5d4d009e20ff8.zip\n  PyhonLibLayer9012B95C is a local Layer in the template\n  Local image is up-to-date\n  Building image.....................\n  Using local image: samcli/lambda-python:3.9-x86_64-df98eaa0c4be380691b9d0fb2.\n\n  Mounting\n  /Users/santosh/Documents/workspace/aws/cdk-sam-demo/cdk.out/asset.5154647dfbc9e4\n  d979237033784da0522eab6dd6532eef010dd6829545c8cb85 as /var/task:ro,delegated,\n  inside runtime container\n  START RequestId: 73cfbe98-e830-44f0-b72e-efb08227af7d Version: $LATEST\n  END RequestId: e45b19ca-7ae7-4239-876f-22484bb3062c\n  REPORT RequestId: e45b19ca-7ae7-4239-876f-22484bb3062c\tInit Duration: 0.46 ms\tDuration: 1501.97 ms\tBilled Duration: 1502 ms\tMemory Size: 256 MB\tMax Memory Used: 256 MB\n  {\"statusCode\": 200, \"body\": \"{\\\"message\\\": \\\"Record created\\\", \\\"record_id\\\": 1}\"}\n  ```\n\n- Read\n\n  ```\n  (base) ➜  cdk-sam-demo git:(main) ✗ sam local invoke MyLambdaFunction --event events/read.json -t ./cdk.out/CdkSamDemoStack.template.json\n  Invoking app.lambda_handler (python3.9)\n  Decompressing\n  /Users/santosh/Documents/workspace/aws/cdk-sam-demo/cdk.out/asset.b9e5b43dd3a267\n  962ccec789725e1117a07177602f17375f57b5d4d009e20ff8.zip\n  PyhonLibLayer9012B95C is a local Layer in the template\n  Local image is up-to-date\n  Building image.....................\n  Using local image: samcli/lambda-python:3.9-x86_64-df98eaa0c4be380691b9d0fb2.\n\n  Mounting\n  /Users/santosh/Documents/workspace/aws/cdk-sam-demo/cdk.out/asset.5154647dfbc9e4\n  d979237033784da0522eab6dd6532eef010dd6829545c8cb85 as /var/task:ro,delegated,\n  inside runtime container\n  START RequestId: 7584b657-d6ea-4720-a0ca-5b659efe58a1 Version: $LATEST\n  END RequestId: 307c58ab-fade-40ab-b28a-085fe2f6ad2c\n  REPORT RequestId: 307c58ab-fade-40ab-b28a-085fe2f6ad2c\tInit Duration: 0.47 ms\tDuration: 1200.22 ms\tBilled Duration: 1201 ms\tMemory Size: 256 MB\tMax Memory Used: 256 MB\n  {\"statusCode\": 200, \"body\": \"{\\\"records\\\": [{\\\"id\\\": 1, \\\"name\\\": \\\"Test Record\\\", \\\"description\\\": \\\"This is a test description.\\\"}]}\"}\n  ```\n\n## Useful commands\n\n- `cdk ls` list all stacks in the app\n- `cdk synth` emits the synthesized CloudFormation template\n- `cdk deploy` deploy this stack to your default AWS account/region\n- `cdk diff` compare deployed stack with current state\n- `cdk docs` open CDK documentation\n\nEnjoy!\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsantoshshinde2012%2Faws-cdk-python-psycopg2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsantoshshinde2012%2Faws-cdk-python-psycopg2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsantoshshinde2012%2Faws-cdk-python-psycopg2/lists"}