ec2.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. "strings"
  19. "github.com/awslabs/goformation/v4/cloudformation"
  20. "github.com/awslabs/goformation/v4/cloudformation/autoscaling"
  21. "github.com/awslabs/goformation/v4/cloudformation/ecs"
  22. "github.com/awslabs/goformation/v4/cloudformation/iam"
  23. "github.com/compose-spec/compose-go/types"
  24. )
  25. const (
  26. placementConstraintAMI = "node.ami == "
  27. placementConstraintMachine = "node.machine == "
  28. )
  29. func (b *ecsAPIService) createCapacityProvider(ctx context.Context, project *types.Project, template *cloudformation.Template, resources awsResources) error {
  30. var (
  31. ec2 bool
  32. ami string
  33. machineType string
  34. )
  35. for _, service := range project.Services {
  36. if requireEC2(service) {
  37. ec2 = true
  38. // TODO once we can assign a service to a CapacityProvider, we could run this _per service_
  39. ami, machineType = getUserDefinedMachine(service)
  40. break
  41. }
  42. }
  43. if !ec2 {
  44. return nil
  45. }
  46. if ami == "" {
  47. recommended, err := b.aws.GetParameter(ctx, "/aws/service/ecs/optimized-ami/amazon-linux-2/gpu/recommended")
  48. if err != nil {
  49. return err
  50. }
  51. ami = recommended
  52. }
  53. if machineType == "" {
  54. t, err := guessMachineType(project)
  55. if err != nil {
  56. return err
  57. }
  58. machineType = t
  59. }
  60. template.Resources["CapacityProvider"] = &ecs.CapacityProvider{
  61. AutoScalingGroupProvider: &ecs.CapacityProvider_AutoScalingGroupProvider{
  62. AutoScalingGroupArn: cloudformation.Ref("AutoscalingGroup"),
  63. ManagedScaling: &ecs.CapacityProvider_ManagedScaling{
  64. TargetCapacity: 100,
  65. },
  66. },
  67. Tags: projectTags(project),
  68. }
  69. template.Resources["AutoscalingGroup"] = &autoscaling.AutoScalingGroup{
  70. LaunchConfigurationName: cloudformation.Ref("LaunchConfiguration"),
  71. MaxSize: "10", //TODO
  72. MinSize: "1",
  73. VPCZoneIdentifier: resources.subnets,
  74. }
  75. userData := base64.StdEncoding.EncodeToString([]byte(
  76. fmt.Sprintf("#!/bin/bash\necho ECS_CLUSTER=%s >> /etc/ecs/ecs.config", project.Name)))
  77. template.Resources["LaunchConfiguration"] = &autoscaling.LaunchConfiguration{
  78. ImageId: ami,
  79. InstanceType: machineType,
  80. SecurityGroups: resources.allSecurityGroups(),
  81. IamInstanceProfile: cloudformation.Ref("EC2InstanceProfile"),
  82. UserData: userData,
  83. }
  84. template.Resources["EC2InstanceProfile"] = &iam.InstanceProfile{
  85. Roles: []string{cloudformation.Ref("EC2InstanceRole")},
  86. }
  87. template.Resources["EC2InstanceRole"] = &iam.Role{
  88. AssumeRolePolicyDocument: ec2InstanceAssumeRolePolicyDocument,
  89. ManagedPolicyArns: []string{
  90. ecsEC2InstanceRole,
  91. },
  92. Tags: projectTags(project),
  93. }
  94. cluster := template.Resources["Cluster"].(*ecs.Cluster)
  95. cluster.CapacityProviders = []string{
  96. cloudformation.Ref("CapacityProvider"),
  97. }
  98. return nil
  99. }
  100. func getUserDefinedMachine(s types.ServiceConfig) (ami string, machineType string) {
  101. if s.Deploy != nil {
  102. for _, s := range s.Deploy.Placement.Constraints {
  103. if strings.HasPrefix(s, placementConstraintAMI) {
  104. ami = s[len(placementConstraintAMI):]
  105. }
  106. if strings.HasPrefix(s, placementConstraintMachine) {
  107. machineType = s[len(placementConstraintMachine):]
  108. }
  109. }
  110. }
  111. return ami, machineType
  112. }