Skip to content

Instantly share code, notes, and snippets.

@satomacoto
Last active October 4, 2022 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satomacoto/4535114350f159746ea0231c4b455aec to your computer and use it in GitHub Desktop.
Save satomacoto/4535114350f159746ea0231c4b455aec to your computer and use it in GitHub Desktop.
Set up ScheduledFargateTask and EFS
def setup_vpc(self):
self.vpc = aws_ec2.Vpc(
self,
"efs-testvpc",
nat_gateways=0,
subnet_configuration=[
aws_ec2.SubnetConfiguration(
name="efs-testsubnet-public",
subnet_type=aws_ec2.SubnetType.PUBLIC,
),
aws_ec2.SubnetConfiguration(
name="efs-testsubnet-isolated",
subnet_type=aws_ec2.SubnetType.PRIVATE_ISOLATED,
),
],
)
self.vpc.add_interface_endpoint(
"efs-test-ecr-endpoint",
service=aws_ec2.InterfaceVpcEndpointAwsService.ECR,
)
self.vpc.add_interface_endpoint(
"efs-test-ecr-docker-endpoint",
service=aws_ec2.InterfaceVpcEndpointAwsService.ECR_DOCKER,
)
self.vpc.add_interface_endpoint(
"efs-test-ecr-logs-endpoint",
service=aws_ec2.InterfaceVpcEndpointAwsService.CLOUDWATCH_LOGS,
)
self.vpc.add_gateway_endpoint(
"efs-test-gateway-endpoint-s3",
service=aws_ec2.GatewayVpcEndpointAwsService.S3,
subnets=[aws_ec2.SubnetSelection(subnets=self.vpc.isolated_subnets)],
)
self.sg = aws_ec2.SecurityGroup(
self,
"efs-test-security-group",
vpc=self.vpc,
security_group_name="efs-test-security-group",
)
def setup_efs(self):
# EFS
self.fs = aws_efs.FileSystem(
self,
"efs-test-fs",
vpc=self.vpc,
security_group=self.sg,
removal_policy=cdk.RemovalPolicy.DESTROY,
lifecycle_policy=aws_efs.LifecyclePolicy.AFTER_7_DAYS,
performance_mode=aws_efs.PerformanceMode.GENERAL_PURPOSE,
file_system_name="efs-test-fs",
)
self.ap = self.fs.add_access_point(
"efs-test-access-point",
create_acl={"owner_gid": "1001", "owner_uid": "1001", "permissions": "777"},
posix_user={"gid": "1001", "uid": "1001"},
path="/data",
)
def setup_scheduled_fargate_task(self):
cluster = aws_ecs.Cluster(
self,
"efs-test-cluster",
cluster_name="efs-test-cluster",
container_insights=True,
vpc=self.vpc,
)
asset = aws_ecr_assets.DockerImageAsset(
self,
"efs-test-image-asset",
directory="./container",
)
scheduled_task = aws_ecs_patterns.ScheduledFargateTask(
self,
"efs-test-fargate-task",
cluster=cluster,
scheduled_fargate_task_image_options=aws_ecs_patterns.ScheduledFargateTaskImageOptions(
image=aws_ecs.ContainerImage.from_docker_image_asset(asset),
cpu=256,
memory_limit_mib=512,
),
schedule=aws_applicationautoscaling.Schedule.expression("rate(1 minute)"),
platform_version=aws_ecs.FargatePlatformVersion.LATEST,
vpc=self.vpc,
subnet_selection=aws_ec2.SubnetSelection(subnets=self.vpc.isolated_subnets),
security_groups=[self.sg],
)
efs_volume_configuration = aws_ecs.EfsVolumeConfiguration(
file_system_id=self.fs.file_system_id,
transit_encryption="ENABLED",
authorization_config=aws_ecs.AuthorizationConfig(
access_point_id=self.ap.access_point_id, iam="ENABLED"
),
)
efs_volume_name = "efs-test-volume"
scheduled_task.task_definition.add_volume(
name=efs_volume_name,
efs_volume_configuration=efs_volume_configuration,
)
scheduled_task.task_definition.default_container.add_mount_points(
aws_ecs.MountPoint(
container_path="/mnt/data/",
read_only=False,
source_volume=efs_volume_name,
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment