func()

in pkg/cfn/cfn.go [50:94]


func (c Cfn) CreateStackAndGetResources(availabilityZones []*ec2.AvailabilityZone,
	stackName *string, template string) (vpcId *string, subnetIds []string, instanceId *string,
	stackResources []*cloudformation.StackResource, err error) {
	if stackName == nil {
		stackName = aws.String(DefaultStackName)
	}

	zonesToUse := []*ec2.AvailabilityZone{}
	if availabilityZones != nil {
		for i := 0; i < RequiredAvailabilityZones; i++ {
			// Wrap around in case of the number of azs is smaller than the required number
			zonesToUse = append(zonesToUse, availabilityZones[i%len(availabilityZones)])
		}
	}

	// Create a new stack
	_, err = c.CreateStack(*stackName, template, zonesToUse)
	if err != nil {
		return nil, nil, nil, nil, err
	}

	// Get all resources in the stack and select the first subnet id
	resources, err := c.GetStackResources(*stackName)
	if err != nil {
		return nil, nil, nil, nil, err
	}

	subnetIds = []string{}
	for _, resource := range resources {
		if *resource.ResourceType == ResourceTypeVpc {
			vpcId = resource.PhysicalResourceId
		} else if *resource.ResourceType == ResourceTypeSubnet {
			subnetIds = append(subnetIds, *resource.PhysicalResourceId)
		} else if *resource.ResourceType == ResourceTypeInstance {
			instanceId = resource.PhysicalResourceId
		}
	}

	// If no VPC or subnet is available, return an error. Note that instanceId is allowed to be nil
	if vpcId == nil || len(subnetIds) <= 0 {
		return nil, nil, nil, nil, errors.New("No enough resources available in the created stack")
	}

	return vpcId, subnetIds, instanceId, resources, nil
}