ec2.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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, template *cloudformation.Template, resources awsResources) 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, err := guessMachineType(project)
  40. if err != nil {
  41. return err
  42. }
  43. template.Resources["CapacityProvider"] = &ecs.CapacityProvider{
  44. AutoScalingGroupProvider: &ecs.CapacityProvider_AutoScalingGroupProvider{
  45. AutoScalingGroupArn: cloudformation.Ref("AutoscalingGroup"),
  46. ManagedScaling: &ecs.CapacityProvider_ManagedScaling{
  47. TargetCapacity: 100,
  48. },
  49. },
  50. Tags: projectTags(project),
  51. }
  52. template.Resources["AutoscalingGroup"] = &autoscaling.AutoScalingGroup{
  53. LaunchConfigurationName: cloudformation.Ref("LaunchConfiguration"),
  54. MaxSize: "10", //TODO
  55. MinSize: "1",
  56. VPCZoneIdentifier: resources.subnets,
  57. }
  58. userData := base64.StdEncoding.EncodeToString([]byte(
  59. fmt.Sprintf("#!/bin/bash\necho ECS_CLUSTER=%s >> /etc/ecs/ecs.config", project.Name)))
  60. template.Resources["LaunchConfiguration"] = &autoscaling.LaunchConfiguration{
  61. ImageId: ami,
  62. InstanceType: machineType,
  63. SecurityGroups: resources.allSecurityGroups(),
  64. IamInstanceProfile: cloudformation.Ref("EC2InstanceProfile"),
  65. UserData: userData,
  66. }
  67. template.Resources["EC2InstanceProfile"] = &iam.InstanceProfile{
  68. Roles: []string{cloudformation.Ref("EC2InstanceRole")},
  69. }
  70. template.Resources["EC2InstanceRole"] = &iam.Role{
  71. AssumeRolePolicyDocument: ec2InstanceAssumeRolePolicyDocument,
  72. ManagedPolicyArns: []string{
  73. ecsEC2InstanceRole,
  74. },
  75. Tags: projectTags(project),
  76. }
  77. cluster := template.Resources["Cluster"].(*ecs.Cluster)
  78. cluster.CapacityProviders = []string{
  79. cloudformation.Ref("CapacityProvider"),
  80. }
  81. return nil
  82. }