public static void DeleteStack(RegionEndpoint awsEndpoint, string stackName) { var codeDeployClient = new AmazonCodeDeployClient(awsEndpoint); var apps = codeDeployClient.ListApplications().Applications.Where(name => name.StartsWith("HelloWorld")); foreach (var app in apps) { codeDeployClient.DeleteApplication(new DeleteApplicationRequest {ApplicationName = app}); } var cloudFormationClient = new AmazonCloudFormationClient(awsEndpoint); try { cloudFormationClient.DeleteStack(new DeleteStackRequest { StackName = stackName }); var testStackStatus = StackStatus.DELETE_IN_PROGRESS; while (testStackStatus == StackStatus.DELETE_IN_PROGRESS) { Thread.Sleep(TimeSpan.FromSeconds(10)); var stacksStatus = cloudFormationClient.DescribeStacks(new DescribeStacksRequest { StackName = stackName }); testStackStatus = stacksStatus.Stacks.First(s => s.StackName == stackName).StackStatus; } } catch (AmazonCloudFormationException) { } }
public Deployer(AwsConfiguration awsConfiguration) { _awsEndpoint = awsConfiguration.AwsEndpoint; _bucket = awsConfiguration.Bucket; _assumeRoleTrustDocument = awsConfiguration.AssumeRoleTrustDocument; _iamRolePolicyDocument = awsConfiguration.IamRolePolicyDocument; AWSCredentials credentials; if (isArn(awsConfiguration.RoleName)) { var securityTokenServiceClient = new AmazonSecurityTokenServiceClient(awsConfiguration.AwsEndpoint); var assumeRoleResult = securityTokenServiceClient.AssumeRole(new AssumeRoleRequest { RoleArn = awsConfiguration.RoleName, DurationSeconds = 3600, RoleSessionName = "Net2User", ExternalId = Guid.NewGuid().ToString() }); Credentials stsCredentials = assumeRoleResult.Credentials; SessionAWSCredentials sessionCredentials = new SessionAWSCredentials(stsCredentials.AccessKeyId, stsCredentials.SecretAccessKey, stsCredentials.SessionToken); credentials = sessionCredentials; _role = new AssumedRole(assumeRoleResult.AssumedRoleUser); } else { credentials = awsConfiguration.Credentials ?? new EnvironmentAWSCredentials(); } _codeDeployClient = new AmazonCodeDeployClient( credentials, new AmazonCodeDeployConfig { RegionEndpoint = awsConfiguration.AwsEndpoint, ProxyHost = awsConfiguration.ProxyHost, ProxyPort = awsConfiguration.ProxyPort }); _cloudFormationClient = new AmazonCloudFormationClient( credentials, new AmazonCloudFormationConfig { RegionEndpoint = awsConfiguration.AwsEndpoint, ProxyHost = awsConfiguration.ProxyHost, ProxyPort = awsConfiguration.ProxyPort }); _s3Client = new AmazonS3Client( credentials, new AmazonS3Config { RegionEndpoint = awsConfiguration.AwsEndpoint, ProxyHost = awsConfiguration.ProxyHost, ProxyPort = awsConfiguration.ProxyPort }); _iamClient = new AmazonIdentityManagementServiceClient( credentials, new AmazonIdentityManagementServiceConfig { RegionEndpoint = awsConfiguration.AwsEndpoint, ProxyHost = awsConfiguration.ProxyHost, ProxyPort = awsConfiguration.ProxyPort }); _autoScalingClient = new AmazonAutoScalingClient( credentials, new AmazonAutoScalingConfig { RegionEndpoint = awsConfiguration.AwsEndpoint, ProxyHost = awsConfiguration.ProxyHost, ProxyPort = awsConfiguration.ProxyPort }); }
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 } } }
public string Push(AmazonS3Client s3Client, AmazonCodeDeployClient codeDeployClient) { var zipFileName = string.Format("{0}.{1}.{2}.zip", ApplicationSetName, Version, BundleName); var tempPath = Path.Combine(Path.GetTempPath(), zipFileName + "." + Guid.NewGuid() + ".zip"); ZipFile.CreateFromDirectory(_bundleDirectory.FullName, tempPath, CompressionLevel.Optimal, false, Encoding.ASCII); var allTheBuckets = s3Client.ListBuckets(new ListBucketsRequest()).Buckets; if (!allTheBuckets.Exists(b => b.BucketName == Bucket)) { s3Client.PutBucket(new PutBucketRequest { BucketName = Bucket, UseClientRegion = true }); } var putResponse = s3Client.PutObject(new PutObjectRequest { BucketName = Bucket, Key = zipFileName, FilePath = tempPath, }); var registration = new RegisterApplicationRevisionRequest { ApplicationName = CodeDeployApplicationName, Description = "Revision " + Version, Revision = new RevisionLocation { RevisionType = RevisionLocationType.S3, S3Location = new S3Location { Bucket = Bucket, BundleType = BundleType.Zip, Key = zipFileName, Version = Version } } }; try { codeDeployClient.RegisterApplicationRevision(registration); } catch (ApplicationDoesNotExistException) { codeDeployClient.CreateApplication(new CreateApplicationRequest { ApplicationName = CodeDeployApplicationName }); codeDeployClient.RegisterApplicationRevision(registration); } return putResponse.ETag; }
public CreateDeploymentResponse DeployToStack( AmazonCodeDeployClient codeDeployClient, AmazonIdentityManagementServiceClient iamClient, AmazonAutoScalingClient autoScalingClient, Role role) { var deploymentGroupName = _stackName + "_" + BundleName; EnsureDeploymentGroupExistsForBundle(codeDeployClient, iamClient, autoScalingClient, role, deploymentGroupName); var deploymentResponse = codeDeployClient.CreateDeployment(new CreateDeploymentRequest { ApplicationName = CodeDeployApplicationName, DeploymentGroupName = deploymentGroupName, Revision = new RevisionLocation { RevisionType = RevisionLocationType.S3, S3Location = new S3Location { Bucket = Bucket, Key = FileName, BundleType = BundleType.Zip, ETag = ETag } } }); return deploymentResponse; }