{"id":25804751,"url":"https://github.com/sergiopichardo/bedrock-image-generation","last_synced_at":"2026-03-02T09:31:21.751Z","repository":{"id":276713652,"uuid":"929626372","full_name":"sergiopichardo/bedrock-image-generation","owner":"sergiopichardo","description":"Using Amazon Bedrock and python to generate images based on a given user prompt","archived":false,"fork":false,"pushed_at":"2025-02-10T02:10:35.000Z","size":303,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-28T01:24:13.833Z","etag":null,"topics":["amazon-api-gateway","aws-lambda","genai","python3","s3-bucket","terraform"],"latest_commit_sha":null,"homepage":"","language":"HCL","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/sergiopichardo.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-02-09T01:28:40.000Z","updated_at":"2025-02-16T23:12:41.000Z","dependencies_parsed_at":"2025-02-10T16:31:34.604Z","dependency_job_id":null,"html_url":"https://github.com/sergiopichardo/bedrock-image-generation","commit_stats":null,"previous_names":["sergiopichardo/bedrock-image-generation"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sergiopichardo/bedrock-image-generation","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sergiopichardo%2Fbedrock-image-generation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sergiopichardo%2Fbedrock-image-generation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sergiopichardo%2Fbedrock-image-generation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sergiopichardo%2Fbedrock-image-generation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sergiopichardo","download_url":"https://codeload.github.com/sergiopichardo/bedrock-image-generation/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sergiopichardo%2Fbedrock-image-generation/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29997213,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-02T01:47:34.672Z","status":"online","status_checked_at":"2026-03-02T02:00:07.342Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["amazon-api-gateway","aws-lambda","genai","python3","s3-bucket","terraform"],"created_at":"2025-02-27T18:53:37.442Z","updated_at":"2026-03-02T09:31:21.715Z","avatar_url":"https://github.com/sergiopichardo.png","language":"HCL","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Amazon Bedrock Image Generation Use Case \n\n## Overview\n\nThis AWS-based image generation pipeline enables users to generate AI-powered images using Amazon Bedrock and store them in Amazon S3. The system is triggered via an API Gateway, which invokes an AWS Lambda function. The Lambda function processes the user’s text prompt, calls Amazon Bedrock's Stability AI model to generate the image, and stores the result in an S3 bucket. A pre-signed URL is then generated and returned to the user, allowing them to securely access their image.\n\n![Architecture](./assets/architecture.png)\n\n## How It Works (Step-by-Step)\n\n### 1. User Submits a Request\n- A user sends a **POST request** via an **API Gateway REST API**, including a **text prompt** for the image they want to generate.  \n- Example request body:  \n  ```json\n  {\n      \"prompt\": \"A futuristic city skyline at sunset\"\n  }\n  ```\n\n\n\n### 2. API Gateway Forwards the Request to AWS Lambda\n- API Gateway acts as an entry point and forwards the request to an **AWS Lambda function**.  \n- The Lambda function is triggered with the event containing the **user prompt**.  \n\n\n\n### 3. Lambda Calls Amazon Bedrock for Image Generation\n- The Lambda function extracts the **text prompt** from the request:  \n  ```python\n  request_body = json.loads(event['body'])\n  image_prompt = request_body['prompt']\n  ```\n- It then **invokes Amazon Bedrock** to generate an image using a **Stability AI model**:  \n  ```python\n  bedrock_response = client_bedrock.invoke_model(\n      contentType='application/json', \n      accept='application/json',\n      modelId=os.environ['BEDROCK_MODEL_ID'],\n      body=json.dumps({\n          \"text_prompts\": [{\"text\": image_prompt}],\n          \"cfg_scale\": 10,\n          \"steps\": 30,\n          \"seed\": 0\n      })\n  )\n  ```\n- The **Bedrock model processes the request** and returns an image in **Base64-encoded format**.  \n\n\n### 4. Lambda Decodes and Stores the Image in S3\n- The Lambda function **extracts and decodes** the **Base64 image data**:  \n  ```python\n  image_response_data = json.loads(bedrock_response['body'].read())\n  image_base64 = image_response_data['artifacts'][0]['base64']\n  decoded_image = base64.b64decode(image_base64)\n  ```\n- It generates a **unique filename** using a timestamp:  \n  ```python\n  image_filename = 'generated-image-' + datetime.datetime.today().strftime('%Y-%m-%d-%H-%M-%S')\n  ```\n- The image is then **stored in an Amazon S3 bucket**:  \n  ```python\n  client_s3.put_object(\n      Bucket=os.environ['BUCKET_NAME'],\n      Body=decoded_image,\n      Key=image_filename\n  )\n  ```\n\n### 5. Lambda Generates a Pre-Signed URL\n- To enable **secure access** to the stored image, Lambda generates a **pre-signed URL**:  \n  ```python\n  presigned_url = client_s3.generate_presigned_url(\n      'get_object',\n      Params={'Bucket': os.environ['BUCKET_NAME'], 'Key': image_filename},\n      ExpiresIn=3600  # URL expires in 1 hour\n  )\n  ```\n- The URL allows the user to **retrieve the image without exposing the S3 bucket publicly**.\n\n\n### 6. Lambda Returns the URL to the User\n- The **pre-signed URL** is sent back as an **API response**:  \n  ```python\n  return {\n      'statusCode': 200,\n      'body': json.dumps({'image_url': presigned_url}),\n      'headers': {'Content-Type': 'application/json'}\n  }\n  ```\n- The user can then **access the generated image** by clicking the returned **pre-signed URL**.\n\n\n## **Key Benefits of This Architecture**  \n- **Serverless \u0026 Scalable** – Uses **API Gateway, Lambda, and S3**, requiring no infrastructure management.  \n- **Secure \u0026 Cost-Effective** – Images are stored privately in **S3**, accessible only via temporary **pre-signed URLs**.  \n- **AI-Powered Generation** – Leverages **Amazon Bedrock’s Stability AI model** for high-quality images.  \n- **Fast \u0026 Efficient** – Images are **generated, stored, and shared** in a single function execution.  \n\n\n\n## Getting Started\n\nInitialize Terraform\n```sh\nterraform init \n```\n\nPlan the deployment\n```sh\nterraform plan -var-file=dev.tfvars\n```\n\nApply the deployment\n```sh\nterraform apply -var-file=dev.tfvars -auto-approve\n```\n\n## Destroy  \n\n```sh\nterraform destroy -var-file=dev.tfvars\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsergiopichardo%2Fbedrock-image-generation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsergiopichardo%2Fbedrock-image-generation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsergiopichardo%2Fbedrock-image-generation/lists"}