-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding Python example for Fargate Service with EFS #1027 #1048
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a9ed5ab
Issue-1027: Initial Commit
2802895
Issue-1027: Added VPC, EFS with policy and access point
d5bdee6
Issue-1027: Adding ECS Cluster with fargate Service and mount point
3531d28
Issue-1027: created efs access point, fargate service, ecs task defin…
b1feb4f
Issue-1027: updated loadbalancer type, re-arrange code blocks
dfed3d8
Issue-1027: removed transit_encryption
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,127 @@ | ||
from aws_cdk import ( | ||
aws_ec2 as ec2, | ||
aws_ecs as ecs, | ||
aws_efs as efs, | ||
aws_iam as iam, | ||
aws_ecs_patterns as ecs_patterns, | ||
App, CfnOutput, Duration, Stack | ||
) | ||
from constructs import Construct | ||
class FargateEfs(Stack): | ||
|
||
def __init__(self, scope: Construct, id: str, **kwargs) -> None: | ||
super().__init__(scope, id, **kwargs) | ||
|
||
#VPC | ||
# Create VPC with 2 AZ and a Fargate Cluster | ||
my_fargate_vpc = ec2.Vpc( | ||
self, "my_fargate_vpc", | ||
max_azs=2 | ||
) | ||
|
||
# EFS | ||
# File System Policy | ||
my_file_system_policy = iam.PolicyDocument( | ||
statements=[iam.PolicyStatement( | ||
actions=["elasticfilesystem:ClientMount"], | ||
principals=[iam.AnyPrincipal()], | ||
resources=["*"], | ||
conditions={ | ||
"Bool": { | ||
"elasticfilesystem:AccessedViaMountTarget": "true" | ||
} | ||
} | ||
)] | ||
) | ||
|
||
# Create EFS in MyFargateVpc | ||
my_file_system = efs.FileSystem(self, "my_fargate_file_system", | ||
vpc=my_fargate_vpc, | ||
encrypted=True, | ||
file_system_policy=my_file_system_policy, | ||
lifecycle_policy=efs.LifecyclePolicy.AFTER_14_DAYS, | ||
performance_mode=efs.PerformanceMode.GENERAL_PURPOSE, | ||
throughput_mode=efs.ThroughputMode.BURSTING, | ||
) | ||
|
||
# Create an access point for the file system | ||
access_point = my_file_system.add_access_point( | ||
"MyAccessPoint", | ||
path="/uploads" | ||
) | ||
|
||
# Output the access point ID and ARN | ||
|
||
CfnOutput( | ||
self, "AccessPointId", | ||
value=access_point.access_point_id, | ||
description="Access Point ID" | ||
) | ||
|
||
CfnOutput( | ||
self, "AccessPointArn", | ||
value=access_point.access_point_arn, | ||
description="Access Point ARN" | ||
) | ||
|
||
# ECS | ||
|
||
# Task Definition | ||
task_definition = ecs.FargateTaskDefinition(self, "MyTaskDefinition") | ||
|
||
# Permissions | ||
my_task_role = iam.Role(self, "TaskRole", assumed_by=iam.ServicePrincipal("ecs-tasks.amazonaws.com")) | ||
my_task_execution_role = iam.Role(self, "TaskExecutionRole", assumed_by=iam.ServicePrincipal("ecs-tasks.amazonaws.com")) | ||
my_file_system.grant_root_access(my_task_execution_role) | ||
|
||
# Add container to the task definition | ||
container = task_definition.add_container( | ||
"WebContainer", | ||
image=ecs.ContainerImage.from_registry("coderaiser/cloudcmd"), | ||
port_mappings=[ | ||
ecs.PortMapping(container_port=8000) | ||
], | ||
memory_limit_mib=512, | ||
cpu=1, | ||
) | ||
|
||
# ECS cluster | ||
cluster = ecs.Cluster( | ||
self, "ECS-EFS-Cluster", | ||
vpc=my_fargate_vpc, | ||
cluster_name="my_ecs_fargate_efs_cluster" | ||
) | ||
|
||
# Create a Fargate service with application load balanced fargte | ||
fargate_service = ecs_patterns.ApplicationLoadBalancedFargateService( | ||
self, "MyFargateService", | ||
cluster=cluster, | ||
task_definition=task_definition, | ||
desired_count=2, | ||
task_subnets=ec2.SubnetSelection( | ||
subnet_type=ec2.SubnetType.PRIVATE_WITH_EGRESS), | ||
assign_public_ip=False, | ||
platform_version=ecs.FargatePlatformVersion.VERSION1_4 | ||
) | ||
|
||
# Mount the EFS filesystem to the fargate service | ||
fargate_service.service.task_definition.default_container.add_mount_points( | ||
ecs.MountPoint( | ||
container_path="/uploads", | ||
source_volume="uploads", | ||
read_only=False | ||
) | ||
) | ||
|
||
|
||
# Output the DNS name of the load balancer | ||
# CfnOutput( | ||
# self, "LoadBalancerDNS", | ||
# value=fargate_service.load_balancer.load_balancer_dns_name, | ||
# description="DNS of the load balancer" | ||
# ) | ||
|
||
|
||
app = App() | ||
FargateEfs(app, "aws-fargate-application-autoscaling") | ||
app.synth() |
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,3 @@ | ||
{ | ||
"app": "python3 app.py" | ||
} |
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,2 @@ | ||
aws-cdk-lib>=2.0.0 | ||
constructs>=10.0.0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using iam.AnyPrincipal(), which is permissive, It might be better to restrict this to specific roles or users.