func()

in internal/pkg/cli/env_init.go [500:587]


func (o *initEnvOpts) askImportResources() error {
	if o.selVPC == nil {
		o.selVPC = selector.NewEC2Select(o.prompt, ec2.New(o.sess))
	}
	if o.importVPC.ID == "" {
		vpcID, err := o.selVPC.VPC(envInitVPCSelectPrompt, "")
		if err != nil {
			if err == selector.ErrVPCNotFound {
				log.Errorf(`No existing VPCs were found. You can either:
- Create a new VPC first and then import it.
- Use the default Copilot environment configuration.
`)
			}
			return fmt.Errorf("select VPC: %w", err)
		}
		o.importVPC.ID = vpcID
	}
	if o.ec2Client == nil {
		o.ec2Client = ec2.New(o.sess)
	}
	dnsSupport, err := o.ec2Client.HasDNSSupport(o.importVPC.ID)
	if err != nil {
		return fmt.Errorf("check if VPC %s has DNS support enabled: %w", o.importVPC.ID, err)
	}
	if !dnsSupport {
		log.Errorln(`Looks like you're creating an environment using a VPC with DNS support *disabled*.
Copilot cannot create services or jobs in VPCs without DNS support. We recommend enabling this property.
To learn more about the issue:
https://aws.amazon.com/premiumsupport/knowledge-center/ecs-pull-container-api-error-ecr/`)
		return fmt.Errorf("VPC %s has no DNS support enabled", o.importVPC.ID)
	}
	if o.importVPC.PublicSubnetIDs == nil {
		publicSubnets, err := o.selVPC.Subnets(selector.SubnetsInput{
			Msg:      envInitPublicSubnetsSelectPrompt,
			Help:     "",
			VPCID:    o.importVPC.ID,
			IsPublic: true,
		})
		if err != nil {
			if errors.Is(err, selector.ErrSubnetsNotFound) {
				log.Warningf(`No existing public subnets were found in VPC %s.
`, o.importVPC.ID)
			} else {
				return fmt.Errorf("select public subnets: %w", err)
			}
		}
		if len(publicSubnets) == 1 {
			return errors.New("select public subnets: at least two public subnets must be selected to enable Load Balancing")
		}
		if len(publicSubnets) == 0 {
			log.Warningf(`If you proceed without public subnets, you will not be able to deploy 
Load Balanced Web Services in this environment, and will need to specify 'private' 
network placement in your workload manifest(s). See the manifest documentation 
specific to your workload type(s) (https://aws.github.io/copilot-cli/docs/manifest/overview/).
`)
		}
		o.importVPC.PublicSubnetIDs = publicSubnets
	}
	if o.importVPC.PrivateSubnetIDs == nil {
		privateSubnets, err := o.selVPC.Subnets(selector.SubnetsInput{
			Msg:      envInitPrivateSubnetsSelectPrompt,
			Help:     "",
			VPCID:    o.importVPC.ID,
			IsPublic: false,
		})
		if err != nil {
			if errors.Is(err, selector.ErrSubnetsNotFound) {
				log.Warningf(`No existing private subnets were found in VPC %s. 
`, o.importVPC.ID)
			} else {
				return fmt.Errorf("select private subnets: %w", err)
			}
		}
		if len(privateSubnets) == 1 {
			return errors.New("select private subnets: at least two private subnets must be selected")
		}
		if len(privateSubnets) == 0 {
			log.Warningf(`If you proceed without private subnets, you will not 
be able to add them after this environment is created.
`)
		}
		o.importVPC.PrivateSubnetIDs = privateSubnets
	}
	if len(o.importVPC.PublicSubnetIDs)+len(o.importVPC.PrivateSubnetIDs) == 0 {
		return errors.New("VPC must have subnets in order to proceed with environment creation")
	}
	return o.validateInternalALBSubnets()
}