-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from aws-solutions-library-samples/v3.2
V3.2 lambdavpcstack
- Loading branch information
Showing
878 changed files
with
8,463 additions
and
1,012 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,6 @@ search-web-knn/node_modules | |
*.pyc | ||
.idea/ | ||
.~*.py | ||
*.ipynb_checkpoints | ||
myenv | ||
venv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/bin/bash | ||
|
||
sudo yum install -y pip npm | ||
sudo npm install -g aws-cdk | ||
|
||
# Get the region of the EC2 instance | ||
TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600") | ||
AZ=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s http://169.254.169.254/latest/meta-data/placement/availability-zone) | ||
REGION=$(echo $AZ | sed 's/[a-zA-Z]$//') | ||
|
||
#pip install based on region | ||
if [[ $REGION == *"cn"* ]]; then | ||
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple | ||
else | ||
pip install -r requirements.txt | ||
fi | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import boto3 | ||
import json | ||
|
||
def lambda_handler(event, context): | ||
# 创建SageMaker客户端 | ||
sagemaker = boto3.client('sagemaker') | ||
|
||
# 调用list_endpoints方法获取端点列表 | ||
response = sagemaker.list_endpoints() | ||
|
||
# 从响应中提取处于"InService"状态的所有端点的名称 | ||
endpoint_names = [ | ||
endpoint['EndpointName'] for endpoint in response['Endpoints'] | ||
if endpoint['EndpointStatus'] == 'InService' | ||
] | ||
|
||
# 返回处于"InService"状态的端点名称列表 | ||
return { | ||
'statusCode': 200, | ||
'headers': { | ||
'Access-Control-Allow-Headers': 'Content-Type', | ||
'Access-Control-Allow-Origin': '*', | ||
'Access-Control-Allow-Methods': 'OPTIONS,GET' | ||
}, | ||
'body': json.dumps(endpoint_names) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import os | ||
import json | ||
import boto3 | ||
from uuid import uuid4 | ||
import time | ||
|
||
TABLE_NAME = os.getenv('TABLE_NAME', '') | ||
PRIMARY_KEY = os.getenv('PRIMARY_KEY', '') | ||
|
||
dynamodb = boto3.resource('dynamodb') | ||
table = dynamodb.Table(TABLE_NAME) | ||
|
||
RESERVED_RESPONSE = "Error: You're using AWS reserved keywords as attributes" | ||
DYNAMODB_EXECUTION_ERROR = "Error: Execution update, caused a Dynamodb error, please take a look at your CloudWatch Logs." | ||
|
||
def update_item(key, update_expression, expression_values, expression_keys): | ||
# Create a DynamoDB resource | ||
dynamodb = boto3.resource('dynamodb') | ||
|
||
# Get the table | ||
table = dynamodb.Table(TABLE_NAME) | ||
|
||
# Update the item | ||
response = table.update_item( | ||
Key=key, | ||
UpdateExpression=update_expression, | ||
ExpressionAttributeValues=expression_values, | ||
ExpressionAttributeNames=expression_keys | ||
) | ||
|
||
return response | ||
|
||
def handler(event, context): | ||
if 'body' not in event: | ||
return {'statusCode': 400, 'body': 'invalid request, you are missing the parameter body'} | ||
|
||
item = event['body'] if isinstance(event['body'], dict) else json.loads(event['body']) | ||
item["jobInfo"]["createdAt"] = int(time.time()) | ||
item = item["jobInfo"] | ||
#add a create time in to item["jobInfo"] use current timestamp | ||
print(f"item:{item}") | ||
if not item.get(id): | ||
item[PRIMARY_KEY] = str(uuid4()) | ||
print(f"item:{item}") | ||
|
||
try: | ||
table.put_item(Item=item) | ||
#return {'statusCode': 201, 'body': f'job id:{item[PRIMARY_KEY]}'} | ||
return { | ||
'statusCode': 200, | ||
'headers': { | ||
'Access-Control-Allow-Headers': 'Content-Type', | ||
'Access-Control-Allow-Origin': '*', | ||
'Access-Control-Allow-Methods': 'OPTIONS,POST,GET' | ||
}, | ||
'body': json.dumps(item) | ||
} | ||
except Exception as e: | ||
error_response = RESERVED_RESPONSE if 'ValidationException' in str(e) and 'reserved keyword' in str(e) else DYNAMODB_EXECUTION_ERROR | ||
return {'statusCode': 500, 'body': error_response} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import os | ||
import json | ||
import boto3 | ||
|
||
TABLE_NAME = os.getenv('TABLE_NAME', '') | ||
PRIMARY_KEY = os.getenv('PRIMARY_KEY', '') | ||
|
||
dynamodb = boto3.resource('dynamodb') | ||
table = dynamodb.Table(TABLE_NAME) | ||
|
||
def handler(event, context): | ||
requested_item_id = event['pathParameters']['id'] if 'pathParameters' in event and 'id' in event['pathParameters'] else None | ||
if not requested_item_id: | ||
return {'statusCode': 400, 'body': 'Error: You are missing the path parameter id'} | ||
|
||
try: | ||
table.delete_item(Key={PRIMARY_KEY: requested_item_id}) | ||
return {'statusCode': 200, 'body': ''} | ||
except Exception as e: | ||
return {'statusCode': 500, 'body': json.dumps(str(e))} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import os | ||
import json | ||
import boto3 | ||
from decimal import Decimal | ||
|
||
class DecimalEncoder(json.JSONEncoder): | ||
def default(self, obj): | ||
if isinstance(obj, Decimal): | ||
return f'{obj}' | ||
return super(DecimalEncoder, self).default(obj) | ||
|
||
TABLE_NAME = os.getenv('TABLE_NAME', '') | ||
|
||
dynamodb = boto3.resource('dynamodb') | ||
table = dynamodb.Table(TABLE_NAME) | ||
|
||
def handler(event, context): | ||
try: | ||
response = table.scan() | ||
return { | ||
'statusCode': 200, | ||
'headers': { | ||
'Access-Control-Allow-Headers': 'Content-Type', | ||
'Access-Control-Allow-Origin': '*', | ||
'Access-Control-Allow-Methods': 'OPTIONS,POST,GET' | ||
}, | ||
'body': json.dumps(response['Items'], cls=DecimalEncoder) | ||
} | ||
except Exception as e: | ||
return { | ||
'statusCode': 500, | ||
'headers': { | ||
'Access-Control-Allow-Headers': 'Content-Type', | ||
'Access-Control-Allow-Origin': '*', | ||
'Access-Control-Allow-Methods': 'OPTIONS,POST,GET' | ||
}, | ||
'body': json.dumps(str(e)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import os | ||
import json | ||
import boto3 | ||
|
||
TABLE_NAME = os.getenv('TABLE_NAME', '') | ||
PRIMARY_KEY = os.getenv('PRIMARY_KEY', '') | ||
|
||
dynamodb = boto3.resource('dynamodb') | ||
table = dynamodb.Table(TABLE_NAME) | ||
|
||
def handler(event, context): | ||
requested_item_id = event['pathParameters']['id'] if 'pathParameters' in event and 'id' in event['pathParameters'] else None | ||
if not requested_item_id: | ||
return {'statusCode': 400, 'body': 'Error: You are missing the path parameter id'} | ||
|
||
try: | ||
response = table.get_item(Key={PRIMARY_KEY: requested_item_id}) | ||
if 'Item' in response: | ||
return {'statusCode': 200, 'body': json.dumps(response['Item'])} | ||
else: | ||
return {'statusCode': 404} | ||
except Exception as e: | ||
return {'statusCode': 500, 'body': json.dumps(str(e))} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import os | ||
import json | ||
import boto3 | ||
|
||
TABLE_NAME = os.getenv('TABLE_NAME', '') | ||
PRIMARY_KEY = os.getenv('PRIMARY_KEY', '') | ||
|
||
dynamodb = boto3.resource('dynamodb') | ||
table = dynamodb.Table(TABLE_NAME) | ||
|
||
def handler(event, context): | ||
if 'body' not in event: | ||
return {'statusCode': 400, 'body': 'invalid request, you are missing the parameter body'} | ||
|
||
print(event) | ||
|
||
edited_item_id = event['pathParameters']['id'] if 'pathParameters' in event and 'id' in event['pathParameters'] else None | ||
if not edited_item_id: | ||
return {'statusCode': 400, 'body': 'invalid request, you are missing the path parameter id'} | ||
|
||
edited_item = event['body'] if isinstance(event['body'], dict) else json.loads(event['body']) | ||
if not edited_item or len(edited_item) < 1: | ||
return {'statusCode': 400, 'body': 'invalid request, no arguments provided'} | ||
|
||
UpdateExpression='SET ' + ', '.join(f'#{k}=:{k}' for k in edited_item.keys()) | ||
ExpressionAttributeValues={f':{k}': v for k, v in edited_item.items()} | ||
ExpressionAttributeNames={f'#{k}': k for k in edited_item.keys()} | ||
|
||
print(UpdateExpression) | ||
print("*************************") | ||
print(ExpressionAttributeValues) | ||
print("*************************") | ||
print(ExpressionAttributeNames) | ||
|
||
try: | ||
table.update_item( | ||
Key={PRIMARY_KEY: edited_item_id}, | ||
UpdateExpression='SET ' + ', '.join(f'#{k}=:{k}' for k in edited_item.keys()), | ||
ExpressionAttributeValues={f':{k}': v for k, v in edited_item.items()}, | ||
ExpressionAttributeNames={f'#{k}': k for k in edited_item.keys()}, | ||
ReturnValues='UPDATED_NEW' | ||
) | ||
return {'statusCode': 200, 'body': ''} | ||
except Exception as e: | ||
return {'statusCode': 500, 'body': json.dumps(str(e))} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
FROM public.ecr.aws/lambda/python:3.9 | ||
|
||
# Copy requirements.txt | ||
COPY requirements.txt ${LAMBDA_TASK_ROOT} | ||
|
||
# Install the specified packages | ||
RUN pip install -r requirements.txt -t ./ | ||
|
||
# Copy function code | ||
COPY . ${LAMBDA_TASK_ROOT} | ||
|
||
# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile) | ||
CMD [ "lambda_function_sqs.lambda_handler" ] |
Oops, something went wrong.