def __init__(self, scope: cdk.Construct, id: str, **kwargs) -> None: super().__init__(scope, id, **kwargs) # DynamoDBの作成 partition_key = dynamodb.Attribute(name='username', type=dynamodb.AttributeType.STRING) self.dynamodb_table = dynamodb.Table( self, 'UsersTable', partition_key=partition_key, removal_policy=cdk.RemovalPolicy.DESTROY) cdk.CfnOutput(self, 'UsersTableName', value=self.dynamodb_table.table_name) # LambdaがDynamoDBにアクセスするためのIAMロールを作成 lambda_service_principal = iam.ServicePrincipal('lambda.amazonaws.com') self.api_handler_iam_role = iam.Role( self, 'ApiHandlerLambdaRole', assumed_by=lambda_service_principal) self.dynamodb_table.grant_read_write_data(self.api_handler_iam_role) # web_api_source_dirはChaliceアプリケーションソースコードへのパス # ソースコードはChaliceによってパッケージングされ、 # SAMテンプレートの作成とLambdaデプロイのためのZIP化が行われる web_api_source_dir = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, 'web-api') chalice_stage_config = self._create_chalice_stage_config() # Chalice作成 self.chalice = Chalice(self, 'WebApi', source_dir=web_api_source_dir, stage_config=chalice_stage_config)
def __init__(self, scope: cdk.Construct, id: str, **kwargs) -> None: super().__init__(scope, id, **kwargs) partition_key = dynamodb.Attribute(name='username', type=dynamodb.AttributeType.STRING) self.dynamodb_table = dynamodb.Table( self, 'UsersTable', partition_key=partition_key, removal_policy=cdk.RemovalPolicy.DESTROY) cdk.CfnOutput(self, 'UsersTableName', value=self.dynamodb_table.table_name) lambda_service_principal = iam.ServicePrincipal('lambda.amazonaws.com') self.api_handler_iam_role = iam.Role( self, 'ApiHandlerLambdaRole', assumed_by=lambda_service_principal) self.dynamodb_table.grant_read_write_data(self.api_handler_iam_role) web_api_source_dir = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, 'web-api') chalice_stage_config = self._create_chalice_stage_config() self.chalice = Chalice(self, 'WebApi', source_dir=web_api_source_dir, stage_config=chalice_stage_config)
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None: super().__init__(scope, id, **kwargs) self.connections_table = dynamodb.Table( self, f"WSPlaygroundConnections", partition_key=dynamodb.Attribute( name="connection_id", type=dynamodb.AttributeType.STRING), billing_mode=dynamodb.BillingMode.PAY_PER_REQUEST, removal_policy=core.RemovalPolicy.DESTROY) self.records_table = dynamodb.Table( self, f"WSPlaygroundRecords", partition_key=dynamodb.Attribute( name="id", type=dynamodb.AttributeType.STRING), billing_mode=dynamodb.BillingMode.PAY_PER_REQUEST, removal_policy=core.RemovalPolicy.DESTROY) core.CfnOutput(self, id="ConnectionsTableName", value=self.connections_table.table_name) core.CfnOutput(self, id="RecordsTableName", value=self.records_table.table_name) self.service_role = self._create_service_role() chalice_dir = os.path.join(os.path.dirname(__file__), os.pardir, "ws") chalice_config = self._create_chalice_stage_config() self.chalice = Chalice(self, id, source_dir=chalice_dir, stage_config=chalice_config)
def test_package_using_subprocess(self) -> None: app = cdk.App(outdir=self.cdk_out_dir) stack = cdk.Stack(app, 'TestSubprocess') chalice = Chalice(stack, 'WebApi', source_dir=self.chalice_app_dir, stage_config=self.chalice_app_stage_config) template = self._synth_and_get_template(app, chalice) self._check_basic_asserts(chalice, template)
def test_package_using_docker_image_not_found(self) -> None: app = cdk.App(outdir=self.cdk_out_dir) stack = cdk.Stack(app, 'TestDockerImageNotFound') package_config = PackageConfig(use_container=True, image='cdk-chalice') with self.assertRaises(ChaliceError): Chalice(stack, 'WebApi', source_dir=self.chalice_app_dir, stage_config=self.chalice_app_stage_config, package_config=package_config)
def test_package_using_docker(self) -> None: app = cdk.App(outdir=self.cdk_out_dir) stack = cdk.Stack(app, 'TestDocker') package_config = PackageConfig(use_container=True) chalice = Chalice(stack, 'WebApi', source_dir=self.chalice_app_dir, stage_config=self.chalice_app_stage_config, package_config=package_config) template = self._synth_and_get_template(app, chalice) self._check_basic_asserts(chalice, template)
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None: super().__init__(scope, id, **kwargs) partition_key = aws_dynamodb.Attribute( name="name", type=aws_dynamodb.AttributeType.STRING) self.items_table = aws_dynamodb.Table(self, f"{id}ItemsTable", partition_key=partition_key) lambda_service_principal = aws_iam.ServicePrincipal( 'lambda.amazonaws.com') self.api_handler_iam_role = aws_iam.Role( self, 'ApiHandlerLambdaRole', assumed_by=lambda_service_principal) self.items_table.grant_read_write_data(self.api_handler_iam_role) chalice_stage_config = self._create_chalice_stage_config() api_source_dir = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, 'api') self.chalice_app = Chalice(self, 'ItemsManagerApi', source_dir=api_source_dir, stage_config=chalice_stage_config)