Skip to content

Commit

Permalink
bouquet tableを diary コンストラクトに移動
Browse files Browse the repository at this point in the history
  • Loading branch information
mt-osho-san committed Jan 1, 2025
1 parent 5c6c264 commit 8e91a7b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 58 deletions.
1 change: 1 addition & 0 deletions src/backend/lib/backend-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export class BackendStack extends cdk.Stack {
const bouquet = new Bouquet(this, 'Bouquet', {
userPool: auth.userPool,
table: flower.table,
bouquetTable: diary.bouquetTable,
api: api.api,
generativeAiTable: flower.generativeAiTable,
cognitoAuthorizer: api.cognitoAuthorizer,
Expand Down
22 changes: 4 additions & 18 deletions src/backend/lib/constructs/bouquet.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import * as cdk from 'aws-cdk-lib'
import * as apigateway from 'aws-cdk-lib/aws-apigateway'
import type * as cognito from 'aws-cdk-lib/aws-cognito'
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb'
import type * as dynamodb from 'aws-cdk-lib/aws-dynamodb'
import * as lambda from 'aws-cdk-lib/aws-lambda'
import * as s3 from 'aws-cdk-lib/aws-s3'
import { Construct } from 'constructs'

export interface BouquetProps {
userPool: cognito.UserPool
table: dynamodb.Table
bouquetTable: dynamodb.Table
api: apigateway.RestApi
generativeAiTable: dynamodb.Table
cognitoAuthorizer: apigateway.CognitoUserPoolsAuthorizer
Expand Down Expand Up @@ -37,21 +38,6 @@ export class Bouquet extends Construct {
})
bouquetBucket.grantRead(bouquetGetFunction)

// 花束の作成情報を保存するDynamoDBテーブルの作成
const bouquetTable = new dynamodb.Table(this, 'BouquetTable', {
partitionKey: {
name: 'user_id',
type: dynamodb.AttributeType.STRING,
},
sortKey: {
name: 'year_week',
type: dynamodb.AttributeType.STRING,
},
removalPolicy: cdk.RemovalPolicy.DESTROY,
pointInTimeRecovery: true,
stream: dynamodb.StreamViewType.NEW_AND_OLD_IMAGES,
})

//花束作成用Lambda関数の定義
const BouquetCreate = new lambda.Function(this, 'BouquetCreate', {
runtime: lambda.Runtime.PYTHON_3_11,
Expand All @@ -65,13 +51,13 @@ export class Bouquet extends Construct {
}),
environment: {
GENERATIVE_AI_TABLE_NAME: props.generativeAiTable.tableName,
BOUQUET_TABLE_NAME: bouquetTable.tableName,
BOUQUET_TABLE_NAME: props.bouquetTable.tableName,
ORIGINAL_IMAGE_BUCKET_NAME: props.originalImageBucket.bucketName,
BOUQUET_BUCKET_NAME: bouquetBucket.bucketName,
},
})
props.generativeAiTable.grantReadData(BouquetCreate)
bouquetTable.grantWriteData(BouquetCreate)
props.bouquetTable.grantWriteData(BouquetCreate)
props.originalImageBucket.grantRead(BouquetCreate)
bouquetBucket.grantPut(BouquetCreate)

Expand Down
21 changes: 20 additions & 1 deletion src/backend/lib/constructs/diary.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as cdk from 'aws-cdk-lib'
import * as apigateway from 'aws-cdk-lib/aws-apigateway'
import type * as cognito from 'aws-cdk-lib/aws-cognito'
import type * as dynamodb from 'aws-cdk-lib/aws-dynamodb'
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb'
import * as lambda from 'aws-cdk-lib/aws-lambda'
import { DynamoEventSource } from 'aws-cdk-lib/aws-lambda-event-sources'
import type * as s3 from 'aws-cdk-lib/aws-s3'
Expand All @@ -20,6 +20,7 @@ export interface DiaryProps {

export class Diary extends Construct {
public readonly diaryTableEventSource: DynamoEventSource
public readonly bouquetTable: dynamodb.Table

constructor(scope: Construct, id: string, props: DiaryProps) {
super(scope, id)
Expand Down Expand Up @@ -178,6 +179,21 @@ export class Diary extends Construct {
authorizer: props.cognitoAuthorizer,
})

// 花束の作成情報を保存するDynamoDBテーブルの作成
const bouquetTable = new dynamodb.Table(this, 'BouquetTable', {
partitionKey: {
name: 'user_id',
type: dynamodb.AttributeType.STRING,
},
sortKey: {
name: 'year_week',
type: dynamodb.AttributeType.STRING,
},
removalPolicy: cdk.RemovalPolicy.DESTROY,
pointInTimeRecovery: true,
stream: dynamodb.StreamViewType.NEW_AND_OLD_IMAGES,
})

const getDiaryDataFunction = new lambda.Function(this, 'getDiaryDataFunction', {
runtime: lambda.Runtime.PYTHON_3_11,
handler: 'get_diary_data.lambda_handler',
Expand All @@ -186,6 +202,7 @@ export class Diary extends Construct {
GENERATIVE_AI_TABLE_NAME: props.generativeAiTable.tableName,
DIARY_TABLE_NAME: props.table.tableName,
FLOWER_BUCKET_NAME: props.flowerBucket.bucketName,
BOUQUET_TABLE_NAME: bouquetTable.tableName,
},
timeout: cdk.Duration.seconds(30),
})
Expand All @@ -198,5 +215,7 @@ export class Diary extends Construct {
diaryDataApi.addMethod('GET', new apigateway.LambdaIntegration(getDiaryDataFunction), {
authorizer: props.cognitoAuthorizer,
})

this.bouquetTable = bouquetTable
}
}
81 changes: 42 additions & 39 deletions src/backend/test/__snapshots__/snapshot.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2076,7 +2076,7 @@ exports[`Snapshot test 1`] = `
"Ref": "BouquetbouquetBucket080B429F",
},
"BOUQUET_TABLE_NAME": {
"Ref": "BouquetBouquetTableE43AF35D",
"Ref": "DiaryBouquetTableF76EF0C5",
},
"GENERATIVE_AI_TABLE_NAME": {
"Ref": "FlowergenerativeAiTable021268D8",
Expand Down Expand Up @@ -2169,7 +2169,7 @@ exports[`Snapshot test 1`] = `
"Resource": [
{
"Fn::GetAtt": [
"BouquetBouquetTableE43AF35D",
"DiaryBouquetTableF76EF0C5",
"Arn",
],
},
Expand Down Expand Up @@ -2245,43 +2245,6 @@ exports[`Snapshot test 1`] = `
},
"Type": "AWS::IAM::Policy",
},
"BouquetBouquetTableE43AF35D": {
"DeletionPolicy": "Delete",
"Properties": {
"AttributeDefinitions": [
{
"AttributeName": "user_id",
"AttributeType": "S",
},
{
"AttributeName": "year_week",
"AttributeType": "S",
},
],
"KeySchema": [
{
"AttributeName": "user_id",
"KeyType": "HASH",
},
{
"AttributeName": "year_week",
"KeyType": "RANGE",
},
],
"PointInTimeRecoverySpecification": {
"PointInTimeRecoveryEnabled": true,
},
"ProvisionedThroughput": {
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5,
},
"StreamSpecification": {
"StreamViewType": "NEW_AND_OLD_IMAGES",
},
},
"Type": "AWS::DynamoDB::Table",
"UpdateReplacePolicy": "Delete",
},
"BouquetbouquetBucket080B429F": {
"DeletionPolicy": "Retain",
"Properties": {
Expand Down Expand Up @@ -2688,6 +2651,43 @@ exports[`Snapshot test 1`] = `
},
"Type": "AWS::IAM::Role",
},
"DiaryBouquetTableF76EF0C5": {
"DeletionPolicy": "Delete",
"Properties": {
"AttributeDefinitions": [
{
"AttributeName": "user_id",
"AttributeType": "S",
},
{
"AttributeName": "year_week",
"AttributeType": "S",
},
],
"KeySchema": [
{
"AttributeName": "user_id",
"KeyType": "HASH",
},
{
"AttributeName": "year_week",
"KeyType": "RANGE",
},
],
"PointInTimeRecoverySpecification": {
"PointInTimeRecoveryEnabled": true,
},
"ProvisionedThroughput": {
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5,
},
"StreamSpecification": {
"StreamViewType": "NEW_AND_OLD_IMAGES",
},
},
"Type": "AWS::DynamoDB::Table",
"UpdateReplacePolicy": "Delete",
},
"DiaryTitleGenerateLambda7612098A": {
"DependsOn": [
"DiarygenerativeAiLambdaRoleDefaultPolicy5FC5F15D",
Expand Down Expand Up @@ -3417,6 +3417,9 @@ exports[`Snapshot test 1`] = `
},
"Environment": {
"Variables": {
"BOUQUET_TABLE_NAME": {
"Ref": "DiaryBouquetTableF76EF0C5",
},
"DIARY_TABLE_NAME": {
"Ref": "FlowerdiaryContentsTableCA7C6940",
},
Expand Down

0 comments on commit 8e91a7b

Please sign in to comment.