void EnsureDeploymentGroupExistsForBundle(AmazonCodeDeployClient codeDeployClient, AmazonIdentityManagementServiceClient iamClient, AmazonAutoScalingClient autoScalingClient, Role role, string deploymentGroupName)
{
var serviceRoleArn = role.Arn;
if (TargetsAutoScalingDeploymentGroup)
{
var group =
autoScalingClient.DescribeAutoScalingGroups()
.AutoScalingGroups.FirstOrDefault(
asg => asg.Tags.Any(t => t.Key == "DeploymentRole" && t.Value == deploymentGroupName));
if (group == null)
throw new ApplicationException(
string.Format("Auto scaling group with DeploymentRole {0} does not exist.", deploymentGroupName));
try
{
codeDeployClient.CreateDeploymentGroup(new CreateDeploymentGroupRequest
{
ApplicationName = CodeDeployApplicationName,
DeploymentGroupName = deploymentGroupName,
ServiceRoleArn = serviceRoleArn,
AutoScalingGroups = new List<string> {group.AutoScalingGroupName}
});
}
catch (DeploymentGroupAlreadyExistsException)
{
// reuse a previously created deployment group with the same name
}
}
else
{
try
{
Console.WriteLine("Will assume role {0} for deployment", serviceRoleArn);
codeDeployClient.CreateDeploymentGroup(new CreateDeploymentGroupRequest
{
ApplicationName = CodeDeployApplicationName,
DeploymentGroupName = deploymentGroupName,
ServiceRoleArn = serviceRoleArn,
Ec2TagFilters = new List<EC2TagFilter>
{
new EC2TagFilter
{
Type = EC2TagFilterType.KEY_AND_VALUE,
Key = "DeploymentRole",
Value = deploymentGroupName
}
}
});
}
catch (DeploymentGroupAlreadyExistsException)
{
// since this is EC2, we can reuse a previously created deployment group with the same name
}
}
}