Skip to content

Instantly share code, notes, and snippets.

@monkey-codes
Last active June 17, 2020 01:03
Show Gist options
  • Select an option

  • Save monkey-codes/76ab7b1c5e27a7c213cfaf270aff5f6d to your computer and use it in GitHub Desktop.

Select an option

Save monkey-codes/76ab7b1c5e27a7c213cfaf270aff5f6d to your computer and use it in GitHub Desktop.
CFN Fargate TaskDefinition and Service
Resource:
...
TaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
Family: !Sub '${Environment}-${ServiceName}'
Cpu: !Ref 'ContainerCpu'
Memory: !Ref 'ContainerMemory'
NetworkMode: awsvpc
RequiresCompatibilities:
- FARGATE
ExecutionRoleArn: {'Fn::ImportValue': !Sub '${ClusterStackName}:ECSTaskExecutionRole'}
TaskRoleArn: !GetAtt TaskExecutionRole.Arn
ContainerDefinitions:
- Name: !Ref 'ServiceName'
Cpu: !Ref 'ContainerCpu'
Memory: !Ref 'ContainerMemory'
Environment:
- Name: AWS_REGION
Value: !Ref "AWS::Region"
- Name: DB_HOST
Value: {'Fn::ImportValue': !Sub '${RDSStackName}:DBClusterEndpointAddress'}
- Name: DB_PORT
Value: {'Fn::ImportValue': !Sub '${RDSStackName}:DBClusterPort'}
- Name: DB_DATABASE
Value: !Sub '${Environment}_${ServiceName}'
- Name: DB_USER
Value: !Sub '${Environment}_${ServiceName}_user'
- Name: DB_PASSWORD_SSM_KEY
Value: !Sub '/${Environment}/${ServiceName}/db/password'
- Name: DB_USERNAME_SSM_KEY
Value: !Sub '/${Environment}/${ServiceName}/db/username'
- Name: SERVICE_KMS_ALIAS
Value: !Sub 'alias/${AWS::StackName}-Key'
Image: !Ref 'ImageUrl'
LogConfiguration:
LogDriver: 'awslogs'
Options:
awslogs-group: !Ref DefaultLogGroup
awslogs-region: !Ref AWS::Region
awslogs-stream-prefix: !Ref Environment
PortMappings:
- ContainerPort: !Ref 'ContainerPort'
Service:
Type: AWS::ECS::Service
DependsOn: [ LoadBalancerRule, CreateServiceDatabase ]
Properties:
ServiceName: !Ref 'ServiceName'
Cluster: {'Fn::ImportValue': !Sub '${ClusterStackName}:ClusterName'}
LaunchType: FARGATE
HealthCheckGracePeriodSeconds: 360
DeploymentConfiguration:
MaximumPercent: 200
MinimumHealthyPercent: 75
DesiredCount: !Ref 'DesiredCount'
NetworkConfiguration:
AwsvpcConfiguration:
AssignPublicIp: DISABLED
SecurityGroups:
- {'Fn::ImportValue': !Sub '${ClusterStackName}:FargateContainerSecurityGroup'}
Subnets:
- {'Fn::ImportValue': !Sub '${NetworkStackName}:PrivateSubnet1'}
- {'Fn::ImportValue': !Sub '${NetworkStackName}:PrivateSubnet2'}
TaskDefinition: !Ref 'TaskDefinition'
LoadBalancers:
- ContainerName: !Ref 'ServiceName'
ContainerPort: !Ref 'ContainerPort'
TargetGroupArn: !Ref 'TargetGroup'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment