Skip to content

Instantly share code, notes, and snippets.

@kkmtyyz
kkmtyyz / s2s_session_manager-add-tool.py
Last active March 21, 2026 20:58
voice-agent-analytics-onprem
async def processToolUse(self, toolName, toolUseContent):
"""Return the tool result"""
logger.info(f"Tool Use Content: {toolUseContent}")
toolName = toolName.lower()
content, result = None, None
try:
if toolUseContent.get("content"):
# Parse the JSON string in the content field
content = toolUseContent.get("content") # Pass the JSON string directly to the agent
@kkmtyyz
kkmtyyz / acquire_lock.py
Last active March 17, 2026 19:42
photo-privacy-protection-system
"""
DynamoDBのステータスを更新して処理ロックを取得する。
S3イベント通知をトリガーとしてこのLambda関数が実行されるため、
ロックを取得することで、同じ写真に対して処理が重複実行されることを防ぐ。
ロック取得条件:
status が CREATED
または
status が PROCESSING かつ processingExpiresAt が現在時刻より過去
@kkmtyyz
kkmtyyz / backend.ts
Last active March 1, 2026 20:52
shape-sync-for-blog
import { defineBackend } from '@aws-amplify/backend';
import * as iam from "aws-cdk-lib/aws-iam";
import { auth } from './auth/resource.js';
import { data } from './data/resource.js';
import { startSfn } from './functions/start-sfn/resource';
import { sendTaskSuccessSfn } from './functions/send-task-success-sfn/resource';
import { AuthorizationType } from 'aws-cdk-lib/aws-appsync'
import { CustomResources } from './custom/resource';
const backend = defineBackend({
@kkmtyyz
kkmtyyz / alb.ts
Last active March 17, 2025 20:02
video-training-service-blog
// S3インターフェースエンドポイントとAPI用Lambda関数のターゲットグループを作成
const s3InterfaceTg = new elbv2.ApplicationTargetGroup(
this,
"S3ITargetGroup",
{
healthCheck: {
// ヘルスチェックの設定
// ref: https://aws.amazon.com/jp/blogs/news/hosting-internal-https-static-websites-with-alb-s3-and-privatelink/
healthyHttpCodes: "200,307,405",
port: "80",
@kkmtyyz
kkmtyyz / AnalyzeLetterFunction.ts
Last active March 21, 2025 22:54
santa-present-delivery-system-blog
this.function = new lambda.Function(this, "AnalyzeLetterFunction", {
handler: "lambda_function.lambda_handler",
runtime: lambda.Runtime.PYTHON_3_12,
timeout: Duration.minutes(15),
memorySize: 1024,
reservedConcurrentExecutions: 10, // Note: Auroraへの接続数に影響
role: this.functionRole,
securityGroups: [this.functionSg],
vpc: spVpc.vpc,
vpcSubnets: { subnets: spVpc.privateSubnets },
this.trainingTable = new dynamodb.TableV2(this, "TrainingTable", {
partitionKey: { name: "TrainingId", type: dynamodb.AttributeType.STRING },
billing: dynamodb.Billing.onDemand(),
removalPolicy: RemovalPolicy.DESTROY,
tableName: config.appName + "Trainings",
});
async function uploadTrainingVideo(uploadUrl) {
try {
const url = uploadUrl;
const req = {
method: "PUT",
headers: {
"Content-Type": "video/mp4",
},
body: trainingVideo,
};
def gen_presigned_url(upload_bucket_name: str, upload_s3key: str) -> str:
region_name = os.environ.get("S3_REGION_NAME")
if region_name is None:
logger.error("Environment variable S3_REGION_NAME not found")
raise
s3 = boto3.client(
"s3", region_name=region_name, config=Config(signature_version="s3v4")
)
try:
/*
* デフォルトで以下2設定が有効
* - プライベートDNS名
* - インバウンドエンドポイントに対してのみのプライベートDNS
*/
this.s3Interface = vtVpc.vpc.addInterfaceEndpoint("S3", {
service: ec2.InterfaceVpcEndpointAwsService.S3,
securityGroups: [this.s3Sg],
subnets: { subnets: vtVpc.privateSubnets },
});
this.uploadBucket = new s3.Bucket(this, "UploadBucket", {
// 署名済みURLからアップロードするバケット
// 名前は何でもいい
bucketName: config.uploadBucketNamePrefix + config.appDomain,
removalPolicy: RemovalPolicy.DESTROY,
});
this.uploadBucket.addToResourcePolicy(
new iam.PolicyStatement({
actions: ["s3:*"],
conditions: {