AWSTemplateFormatVersion: '2010-09-09' Description: >- Odara isolated runs on AWS Fargate: an ECS cluster, a runner task definition, its log group, a small network with no inbound access, a bucket for large run bundles, and an IAM user for the Odara server that may only start, stop and watch these tasks, read their logs and manage the per-run secrets. Parameters: RunnerImage: Type: String Default: public.ecr.aws/z0x0e1s3/odara-runner:0.4.4 Description: >- The odara-runner image. The default is the public image of this Odara release; use the tag of the Odara version your server runs. AllowedPattern: '^[A-Za-z0-9._/:@-]+$' TaskCpu: Type: String Default: '1024' AllowedValues: ['256', '512', '1024', '2048', '4096', '8192', '16384'] Description: Default task CPU units (1024 = 1 vCPU); each Odara environment may ask for another size. TaskMemory: Type: String Default: '2048' Description: Default task memory (MB); must be a size Fargate accepts with TaskCpu. LogRetentionDays: Type: Number Default: 14 AllowedValues: [1, 3, 5, 7, 14, 30, 60, 90] VpcCidr: Type: String Default: 10.42.0.0/16 Description: Address range of the network created for the runs. Resources: # ---- Network: two public subnets, outbound only (no NAT gateway: tasks get # a public IP to pull the image and reach the databases and AWS APIs). Vpc: Type: AWS::EC2::VPC Properties: CidrBlock: !Ref VpcCidr EnableDnsSupport: true EnableDnsHostnames: true Tags: [{ Key: Name, Value: odara-runs }] InternetGateway: Type: AWS::EC2::InternetGateway Properties: Tags: [{ Key: Name, Value: odara-runs }] GatewayAttachment: Type: AWS::EC2::VPCGatewayAttachment Properties: VpcId: !Ref Vpc InternetGatewayId: !Ref InternetGateway RouteTable: Type: AWS::EC2::RouteTable Properties: VpcId: !Ref Vpc Tags: [{ Key: Name, Value: odara-runs }] DefaultRoute: Type: AWS::EC2::Route DependsOn: GatewayAttachment Properties: RouteTableId: !Ref RouteTable DestinationCidrBlock: 0.0.0.0/0 GatewayId: !Ref InternetGateway SubnetA: Type: AWS::EC2::Subnet Properties: VpcId: !Ref Vpc CidrBlock: !Select [0, !Cidr [!Ref VpcCidr, 2, 8]] AvailabilityZone: !Select [0, !GetAZs ''] Tags: [{ Key: Name, Value: odara-runs-a }] SubnetB: Type: AWS::EC2::Subnet Properties: VpcId: !Ref Vpc CidrBlock: !Select [1, !Cidr [!Ref VpcCidr, 2, 8]] AvailabilityZone: !Select [1, !GetAZs ''] Tags: [{ Key: Name, Value: odara-runs-b }] SubnetARoutes: Type: AWS::EC2::SubnetRouteTableAssociation Properties: SubnetId: !Ref SubnetA RouteTableId: !Ref RouteTable SubnetBRoutes: Type: AWS::EC2::SubnetRouteTableAssociation Properties: SubnetId: !Ref SubnetB RouteTableId: !Ref RouteTable RunSecurityGroup: Type: AWS::EC2::SecurityGroup Properties: GroupDescription: Odara run tasks - no inbound, outbound allowed VpcId: !Ref Vpc SecurityGroupEgress: - IpProtocol: '-1' CidrIp: 0.0.0.0/0 Description: Image pull, AWS APIs, pipeline databases and services Tags: [{ Key: Name, Value: odara-runs }] # ---- ECS Cluster: Type: AWS::ECS::Cluster Properties: ClusterName: !Sub 'odara-${AWS::StackName}' CapacityProviders: [FARGATE] ClusterSettings: - Name: containerInsights Value: disabled # Run logs are kept LogRetentionDays, then go with the stack. RunLogGroup: Type: AWS::Logs::LogGroup DeletionPolicy: Delete UpdateReplacePolicy: Delete Properties: LogGroupName: !Sub '/odara/runs/${AWS::StackName}' RetentionInDays: !Ref LogRetentionDays # Only short-lived copies of run bundles: deleted with the stack. BundleBucket: Type: AWS::S3::Bucket DeletionPolicy: Delete UpdateReplacePolicy: Delete Properties: PublicAccessBlockConfiguration: BlockPublicAcls: true BlockPublicPolicy: true IgnorePublicAcls: true RestrictPublicBuckets: true BucketEncryption: ServerSideEncryptionConfiguration: - ServerSideEncryptionByDefault: { SSEAlgorithm: AES256 } OwnershipControls: Rules: [{ ObjectOwnership: BucketOwnerEnforced }] LifecycleConfiguration: Rules: - Id: expire-leftover-bundles Status: Enabled Prefix: odara-runs/ ExpirationInDays: 1 # What ECS needs to start the task: write its logs (the image is public). ExecutionRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: { Service: ecs-tasks.amazonaws.com } Action: sts:AssumeRole Condition: ArnLike: aws:SourceArn: !Sub 'arn:${AWS::Partition}:ecs:${AWS::Region}:${AWS::AccountId}:*' StringEquals: aws:SourceAccount: !Ref AWS::AccountId Policies: - PolicyName: write-run-logs PolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Action: [logs:CreateLogStream, logs:PutLogEvents] Resource: !Sub '${RunLogGroup.Arn}' # What the runner may do: read its own bundle and secrets, nothing else. TaskRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: { Service: ecs-tasks.amazonaws.com } Action: sts:AssumeRole Condition: ArnLike: aws:SourceArn: !Sub 'arn:${AWS::Partition}:ecs:${AWS::Region}:${AWS::AccountId}:*' StringEquals: aws:SourceAccount: !Ref AWS::AccountId Policies: - PolicyName: read-run-inputs PolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Action: secretsmanager:GetSecretValue Resource: !Sub 'arn:${AWS::Partition}:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:odara/run/*' - Effect: Allow Action: s3:GetObject Resource: !Sub '${BundleBucket.Arn}/odara-runs/*' RunnerTaskDefinition: Type: AWS::ECS::TaskDefinition Properties: Family: !Sub 'odara-runner-${AWS::StackName}' RequiresCompatibilities: [FARGATE] NetworkMode: awsvpc Cpu: !Ref TaskCpu Memory: !Ref TaskMemory RuntimePlatform: CpuArchitecture: X86_64 OperatingSystemFamily: LINUX ExecutionRoleArn: !GetAtt ExecutionRole.Arn TaskRoleArn: !GetAtt TaskRole.Arn ContainerDefinitions: - Name: runner Image: !Ref RunnerImage Essential: true StopTimeout: 30 LinuxParameters: InitProcessEnabled: false Capabilities: Drop: [ALL] LogConfiguration: LogDriver: awslogs Options: awslogs-group: !Ref RunLogGroup awslogs-region: !Ref AWS::Region awslogs-stream-prefix: odara # ---- The Odara server's identity (it runs outside AWS): an IAM user whose # only rights are the ones below. Create its access key in the console. ServerPolicy: Type: AWS::IAM::ManagedPolicy Properties: Description: Odara server - launch, watch and stop isolated runs PolicyDocument: Version: '2012-10-17' Statement: - Sid: RunTheRunnerOnThisCluster Effect: Allow Action: ecs:RunTask Resource: !Sub 'arn:${AWS::Partition}:ecs:${AWS::Region}:${AWS::AccountId}:task-definition/odara-runner-${AWS::StackName}:*' Condition: ArnEquals: ecs:cluster: !GetAtt Cluster.Arn - Sid: TagTheTasksItStarts Effect: Allow Action: ecs:TagResource Resource: !Sub 'arn:${AWS::Partition}:ecs:${AWS::Region}:${AWS::AccountId}:task/${Cluster}/*' Condition: StringEquals: ecs:CreateAction: RunTask - Sid: WatchAndStopItsTasks Effect: Allow Action: [ecs:DescribeTasks, ecs:StopTask] Resource: !Sub 'arn:${AWS::Partition}:ecs:${AWS::Region}:${AWS::AccountId}:task/${Cluster}/*' Condition: ArnEquals: ecs:cluster: !GetAtt Cluster.Arn - Sid: HandTheRolesToTheTask Effect: Allow Action: iam:PassRole Resource: [!GetAtt TaskRole.Arn, !GetAtt ExecutionRole.Arn] Condition: StringEquals: iam:PassedToService: ecs-tasks.amazonaws.com - Sid: ReadRunLogs Effect: Allow Action: logs:GetLogEvents Resource: !Sub 'arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:${RunLogGroup}:log-stream:*' - Sid: PerRunSecrets Effect: Allow Action: [secretsmanager:CreateSecret, secretsmanager:DeleteSecret, secretsmanager:TagResource] Resource: !Sub 'arn:${AWS::Partition}:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:odara/run/*' - Sid: LargeBundles Effect: Allow Action: [s3:PutObject, s3:DeleteObject] Resource: !Sub '${BundleBucket.Arn}/odara-runs/*' ServerUser: Type: AWS::IAM::User Properties: UserName: !Sub 'odara-server-${AWS::StackName}' ManagedPolicyArns: [!Ref ServerPolicy] Outputs: OdaraFargateSetting: Description: Paste these values in Odara (Admin > Environments > Execution > Isolated (AWS Fargate)). Value: !Sub >- {"mode":"fargate","region":"${AWS::Region}","cluster":"${Cluster}", "task_definition":"odara-runner-${AWS::StackName}","subnets":["${SubnetA}","${SubnetB}"], "security_groups":["${RunSecurityGroup}"],"assign_public_ip":true, "log_group":"${RunLogGroup}","bundle_bucket":"${BundleBucket}","cpus":1,"memory_mb":2048,"timeout_secs":3600} Region: Value: !Ref AWS::Region Cluster: Value: !Ref Cluster TaskDefinition: Value: !Sub 'odara-runner-${AWS::StackName}' Subnets: Value: !Sub '${SubnetA},${SubnetB}' SecurityGroup: Value: !Ref RunSecurityGroup LogGroup: Value: !Ref RunLogGroup BundleBucket: Value: !Ref BundleBucket ServerUser: Description: Create an access key for this user (IAM > Users > Security credentials) and give it to the Odara server. Value: !Ref ServerUser