Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
Binary file added Product_Service2/.DS_Store
Binary file not shown.
File renamed without changes.
5 changes: 5 additions & 0 deletions .gitignore → Product_Service2/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ __pycache__
# CDK asset staging directory
.cdk.staging
cdk.out

# Enviroment files
.env
*.env
.env.*
14 changes: 14 additions & 0 deletions Product_Service2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Environment Variables

The following environment variables are required:

- `AWS_REGION`: AWS region (default: us-east-1)
- `PRODUCTS_TABLE_NAME`: DynamoDB products table name
- `STOCKS_TABLE_NAME`: DynamoDB stocks table name
- `AWS_ACCESS_KEY_ID`: AWS access key
- `AWS_SECRET_ACCESS_KEY`: AWS secret access key

Copy `.env.example` to `.env` and fill in your values:

```bash
cp .env.example .env
File renamed without changes.
File renamed without changes.
File renamed without changes.
239 changes: 239 additions & 0 deletions Product_Service2/product_service/api_gateway_stack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
""" from aws_cdk import (
Stack,
aws_apigateway as apigw,
CfnOutput
)
from constructs import Construct

class ApiGatewayStack(Stack):
def __init__(self, scope: Construct, construct_id: str, lambda_stack, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)

# Create API Gateway with CORS
api = apigw.RestApi(
self, 'ProductsApi',
rest_api_name='Products Service',
default_cors_preflight_options=apigw.CorsOptions(
allow_origins=apigw.Cors.ALL_ORIGINS,
allow_methods=apigw.Cors.ALL_METHODS,
allow_headers=[
'Content-Type',
'X-Amz-Date',
'Authorization',
'X-Api-Key',
'X-Amz-Security-Token',
],
)
)

# Create API resources and methods
products = api.root.add_resource('products')
products.add_method(
'GET',
apigw.LambdaIntegration(
lambda_stack.list_products_function,
proxy=True,
integration_responses=[{
'statusCode': '200',
'responseParameters': {
'method.response.header.Access-Control-Allow-Origin': "'*'"
}
}]
),
method_responses=[{
'statusCode': '200',
'responseParameters': {
'method.response.header.Access-Control-Allow-Origin': True
}
}]
)

product = products.add_resource('{productId}')
product.add_method(
'GET',
apigw.LambdaIntegration(
lambda_stack.get_product_function,
proxy=True,
integration_responses=[{
'statusCode': '200',
'responseParameters': {
'method.response.header.Access-Control-Allow-Origin': "'*'"
}
}]
),
method_responses=[{
'statusCode': '200',
'responseParameters': {
'method.response.header.Access-Control-Allow-Origin': True
}
}]
)

# Output the API URL
CfnOutput(
self, "APIGatewayURL",
value=f"{api.url}products",
description="API Gateway endpoint URL"
)
"""
from aws_cdk import (
Stack,
aws_apigateway as apigw,
CfnOutput
)
from constructs import Construct

class ApiGatewayStack(Stack):
def __init__(self, scope: Construct, construct_id: str, lambda_stack, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)

# Create API Gateway with CORS
api = apigw.RestApi(
self, 'ProductsApi',
rest_api_name='Products Service',
default_cors_preflight_options=apigw.CorsOptions(
allow_origins=apigw.Cors.ALL_ORIGINS,
allow_methods=apigw.Cors.ALL_METHODS,
allow_headers=[
'Content-Type',
'X-Amz-Date',
'Authorization',
'X-Api-Key',
'X-Amz-Security-Token',
],
)
)

# Common response configurations
cors_response_parameters = {
'method.response.header.Access-Control-Allow-Origin': "'*'"
}

success_response = {
'statusCode': '200',
'responseParameters': cors_response_parameters
}

error_responses = [
{
'statusCode': '400',
'responseParameters': cors_response_parameters
},
{
'statusCode': '500',
'responseParameters': cors_response_parameters
}
]

method_responses = [
{
'statusCode': '200',
'responseParameters': {
'method.response.header.Access-Control-Allow-Origin': True
}
},
{
'statusCode': '400',
'responseParameters': {
'method.response.header.Access-Control-Allow-Origin': True
}
},
{
'statusCode': '500',
'responseParameters': {
'method.response.header.Access-Control-Allow-Origin': True
}
}
]

# Create API resources and methods
products = api.root.add_resource('products')

# GET /products
products.add_method(
'GET',
apigw.LambdaIntegration(
lambda_stack.list_products_function,
proxy=True,
integration_responses=[success_response, *error_responses]
),
method_responses=method_responses
)

# POST /products
products.add_method(
'POST',
apigw.LambdaIntegration(
lambda_stack.product_function,
proxy=True,
integration_responses=[
{
'statusCode': '201',
'responseParameters': cors_response_parameters
},
*error_responses
]
),
method_responses=[
{
'statusCode': '201',
'responseParameters': {
'method.response.header.Access-Control-Allow-Origin': True
}
},
*method_responses[1:] # Include 400 and 500 responses
]
)

# GET /products/{productId}
product = products.add_resource('{productId}')
product.add_method(
'GET',
apigw.LambdaIntegration(
lambda_stack.get_product_function,
proxy=True,
integration_responses=[
success_response,
{
'statusCode': '404',
'responseParameters': cors_response_parameters
},
*error_responses
]
),
method_responses=[
*method_responses,
{
'statusCode': '404',
'responseParameters': {
'method.response.header.Access-Control-Allow-Origin': True
}
}
]
)

# Output the API URL
CfnOutput(
self, "APIGatewayURL",
value=f"{api.url}products",
description="API Gateway endpoint URL"
)

# Output separate endpoints for each operation
CfnOutput(
self, "GetProductsURL",
value=f"{api.url}products",
description="GET products endpoint URL"
)

CfnOutput(
self, "CreateProductURL",
value=f"{api.url}products",
description="POST product endpoint URL"
)

CfnOutput(
self, "GetProductByIdURL",
value=f"{api.url}products/{{productId}}",
description="GET product by ID endpoint URL"
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
)
from constructs import Construct

def create_get_product_lambda(scope: Construct, id: str) -> _lambda.Function:
def create_get_product_lambda(scope: Construct,
id: str, environment: dict, role: None) -> _lambda.Function:
return _lambda.Function(
scope,
id,
runtime=_lambda.Runtime.PYTHON_3_9,
handler='product_by_id.handler',
code=_lambda.Code.from_asset('product_service/lambda_func')
code=_lambda.Code.from_asset('product_service/lambda_func'),
environment=environment or {}
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
)
from constructs import Construct

def create_list_products_lambda(scope: Construct, id: str) -> _lambda.Function:
def create_list_products_lambda(scope: Construct, id: str, environment: dict, role: None) -> _lambda.Function:
return _lambda.Function(
scope,
id,
runtime=_lambda.Runtime.PYTHON_3_9,
handler='product_list.handler',
code=_lambda.Code.from_asset('product_service/lambda_func')
code=_lambda.Code.from_asset('product_service/lambda_func'),
environment=environment or {}
)
Loading