ec2.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package ecs
  14. import (
  15. "context"
  16. "encoding/base64"
  17. "fmt"
  18. "github.com/awslabs/goformation/v4/cloudformation"
  19. "github.com/awslabs/goformation/v4/cloudformation/autoscaling"
  20. "github.com/awslabs/goformation/v4/cloudformation/ecs"
  21. "github.com/awslabs/goformation/v4/cloudformation/iam"
  22. "github.com/compose-spec/compose-go/types"
  23. )
  24. func (b *ecsAPIService) createCapacityProvider(ctx context.Context, project *types.Project, networks map[string]string, template *cloudformation.Template) error {
  25. var ec2 bool
  26. for _, s := range project.Services {
  27. if requireEC2(s) {
  28. ec2 = true
  29. break
  30. }
  31. }
  32. if !ec2 {
  33. return nil
  34. }
  35. ami, err := b.SDK.GetParameter(ctx, "/aws/service/ecs/optimized-ami/amazon-linux-2/gpu/recommended")
  36. if err != nil {
  37. return err
  38. }
  39. machineType := "g4dn.xlarge" // FIXME https://github.com/docker/compose-cli/pull/628
  40. var securityGroups []string
  41. for _, r := range networks {
  42. securityGroups = append(securityGroups, r)
  43. }
  44. template.Resources["CapacityProvider"] = &ecs.CapacityProvider{
  45. AutoScalingGroupProvider: &ecs.CapacityProvider_AutoScalingGroupProvider{
  46. AutoScalingGroupArn: cloudformation.Ref("AutoscalingGroup"),
  47. ManagedScaling: &ecs.CapacityProvider_ManagedScaling{
  48. TargetCapacity: 100,
  49. },
  50. },
  51. Tags: projectTags(project),
  52. AWSCloudFormationCondition: "CreateCluster",
  53. }
  54. template.Resources["AutoscalingGroup"] = &autoscaling.AutoScalingGroup{
  55. LaunchConfigurationName: cloudformation.Ref("LaunchConfiguration"),
  56. MaxSize: "10", //TODO
  57. MinSize: "1",
  58. VPCZoneIdentifier: []string{
  59. cloudformation.Ref(parameterSubnet1Id),
  60. cloudformation.Ref(parameterSubnet2Id),
  61. },
  62. AWSCloudFormationCondition: "CreateCluster",
  63. }
  64. userData := base64.StdEncoding.EncodeToString([]byte(
  65. fmt.Sprintf("#!/bin/bash\necho ECS_CLUSTER=%s >> /etc/ecs/ecs.config", project.Name)))
  66. template.Resources["LaunchConfiguration"] = &autoscaling.LaunchConfiguration{
  67. ImageId: ami,
  68. InstanceType: machineType,
  69. SecurityGroups: securityGroups,
  70. IamInstanceProfile: cloudformation.Ref("EC2InstanceProfile"),
  71. UserData: userData,
  72. AWSCloudFormationCondition: "CreateCluster",
  73. }
  74. template.Resources["EC2InstanceProfile"] = &iam.InstanceProfile{
  75. Roles: []string{cloudformation.Ref("EC2InstanceRole")},
  76. AWSCloudFormationCondition: "CreateCluster",
  77. }
  78. template.Resources["EC2InstanceRole"] = &iam.Role{
  79. AssumeRolePolicyDocument: ec2InstanceAssumeRolePolicyDocument,
  80. ManagedPolicyArns: []string{
  81. ecsEC2InstanceRole,
  82. },
  83. Tags: projectTags(project),
  84. AWSCloudFormationCondition: "CreateCluster",
  85. }
  86. cluster := template.Resources["Cluster"].(*ecs.Cluster)
  87. cluster.CapacityProviders = []string{
  88. cloudformation.Ref("CapacityProvider"),
  89. }
  90. return nil
  91. }