public SpaStack(Construct scope, string id, SpaStackProps props) : base(scope, id, props) { //s3 bucket var bucket = new Bucket(this, $"{props.ServiceName}-bucket", new BucketProps { WebsiteIndexDocument = "index.html", Versioned = true, BucketName = props.ServiceName, RemovalPolicy = RemovalPolicy.DESTROY }); //cloudfront distribution var cloudFrontOai = new OriginAccessIdentity(this, $"{props.ServiceName}-oai", new OriginAccessIdentityProps { Comment = $"OAI for {props.ServiceName}." }); var cloudfrontDist = new CloudFrontWebDistribution(this, $"{props.ServiceName}-cfd", new CloudFrontWebDistributionProps { ViewerCertificate = ViewerCertificate.FromAcmCertificate( props.CloudFrontCert, new ViewerCertificateOptions { Aliases = new [] { $"{props.SubDomain}.{props.HostedZoneName}" }, SslMethod = SSLMethod.SNI }), OriginConfigs = new ISourceConfiguration[] { new SourceConfiguration { S3OriginSource = new S3OriginConfig { S3BucketSource = bucket, OriginAccessIdentity = cloudFrontOai }, Behaviors = new IBehavior[] { new Behavior { IsDefaultBehavior = true, } } } } }); var cnameRecord = new CnameRecord(this, $"{props.ServiceName}CloudFrontCname", new CnameRecordProps { Zone = HostedZone.FromHostedZoneAttributes(this, "HostedZone", new HostedZoneAttributes { ZoneName = props.HostedZoneName, HostedZoneId = props.HostedZoneId }), RecordName = props.SubDomain, DomainName = cloudfrontDist.DistributionDomainName }); var cloudfrontS3Access = new PolicyStatement(); cloudfrontS3Access.AddActions("s3:GetBucket*", "s3:GetObject*", "s3:List*"); cloudfrontS3Access.AddResources(bucket.BucketArn); cloudfrontS3Access.AddResources($"{bucket.BucketArn}/*"); cloudfrontS3Access.AddCanonicalUserPrincipal(cloudFrontOai.CloudFrontOriginAccessIdentityS3CanonicalUserId); bucket.AddToResourcePolicy(cloudfrontS3Access); //codebuild project var codeBuildProject = new Project(this, $"{props.ServiceName}-codeBuild-project", new ProjectProps { Vpc = props.Vpc, ProjectName = props.ServiceName, Environment = new BuildEnvironment { BuildImage = LinuxBuildImage.STANDARD_4_0, }, Source = Source.GitHub(props.GitHubSourceProps), BuildSpec = BuildSpec.FromSourceFilename(props.BuildSpecFile), EnvironmentVariables = new Dictionary <string, IBuildEnvironmentVariable> { { "SPA_DIRECTORY", new BuildEnvironmentVariable { Value = props.SpaDirectory } }, { "S3_BUCKET", new BuildEnvironmentVariable { Value = bucket.BucketName } }, { "CLOUDFRONT_ID", new BuildEnvironmentVariable { Value = cloudfrontDist.DistributionId } }, { "API_URL", new BuildEnvironmentVariable { Value = props.ApiUrl } } } }); // iam policy to push your build to S3 codeBuildProject.AddToRolePolicy( new PolicyStatement(new PolicyStatementProps { Effect = Effect.ALLOW, Resources = new[] { bucket.BucketArn, $"{bucket.BucketArn}/*" }, Actions = new[] { "s3:GetBucket*", "s3:List*", "s3:GetObject*", "s3:DeleteObject", "s3:PutObject" } })); codeBuildProject.AddToRolePolicy( new PolicyStatement(new PolicyStatementProps { Effect = Effect.ALLOW, Resources = new [] { "*" }, Actions = new [] { "cloudfront:CreateInvalidation", "cloudfront:GetDistribution*", "cloudfront:GetInvalidation", "cloudfront:ListInvalidations", "cloudfront:ListDistributions" } })); //codepipeline? }
public IRequest Marshall(CreateDistributionRequest createDistributionRequest) { IRequest request = new DefaultRequest(createDistributionRequest, "AmazonCloudFront"); request.HttpMethod = "POST"; string uriResourcePath = "2013-08-26/distribution"; if (uriResourcePath.Contains("?")) { string queryString = uriResourcePath.Substring(uriResourcePath.IndexOf("?") + 1); uriResourcePath = uriResourcePath.Substring(0, uriResourcePath.IndexOf("?")); foreach (string s in queryString.Split('&', ';')) { string[] nameValuePair = s.Split('='); if (nameValuePair.Length == 2 && nameValuePair[1].Length > 0) { request.Parameters.Add(nameValuePair[0], nameValuePair[1]); } else { request.Parameters.Add(nameValuePair[0], null); } } } request.ResourcePath = uriResourcePath; StringWriter stringWriter = new StringWriter(); XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter); xmlWriter.Namespaces = true; if (createDistributionRequest != null) { DistributionConfig distributionConfigDistributionConfig = createDistributionRequest.DistributionConfig; if (distributionConfigDistributionConfig != null) { xmlWriter.WriteStartElement("DistributionConfig", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); if (distributionConfigDistributionConfig.IsSetCallerReference()) { xmlWriter.WriteElementString("CallerReference", "http://cloudfront.amazonaws.com/doc/2013-08-26/", distributionConfigDistributionConfig.CallerReference.ToString()); } if (distributionConfigDistributionConfig != null) { Aliases aliasesAliases = distributionConfigDistributionConfig.Aliases; if (aliasesAliases != null) { xmlWriter.WriteStartElement("Aliases", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); if (aliasesAliases.IsSetQuantity()) { xmlWriter.WriteElementString("Quantity", "http://cloudfront.amazonaws.com/doc/2013-08-26/", aliasesAliases.Quantity.ToString()); } if (aliasesAliases != null) { List <string> aliasesAliasesitemsList = aliasesAliases.Items; if (aliasesAliasesitemsList != null && aliasesAliasesitemsList.Count > 0) { int aliasesAliasesitemsListIndex = 1; xmlWriter.WriteStartElement("Items", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); foreach (string aliasesAliasesitemsListValue in aliasesAliasesitemsList) { xmlWriter.WriteStartElement("CNAME", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); xmlWriter.WriteValue(aliasesAliasesitemsListValue); xmlWriter.WriteEndElement(); aliasesAliasesitemsListIndex++; } xmlWriter.WriteEndElement(); } } xmlWriter.WriteEndElement(); } } if (distributionConfigDistributionConfig.IsSetDefaultRootObject()) { xmlWriter.WriteElementString("DefaultRootObject", "http://cloudfront.amazonaws.com/doc/2013-08-26/", distributionConfigDistributionConfig.DefaultRootObject.ToString()); } if (distributionConfigDistributionConfig != null) { Origins originsOrigins = distributionConfigDistributionConfig.Origins; if (originsOrigins != null) { xmlWriter.WriteStartElement("Origins", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); if (originsOrigins.IsSetQuantity()) { xmlWriter.WriteElementString("Quantity", "http://cloudfront.amazonaws.com/doc/2013-08-26/", originsOrigins.Quantity.ToString()); } if (originsOrigins != null) { List <Origin> originsOriginsitemsList = originsOrigins.Items; if (originsOriginsitemsList != null && originsOriginsitemsList.Count > 0) { int originsOriginsitemsListIndex = 1; xmlWriter.WriteStartElement("Items", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); foreach (Origin originsOriginsitemsListValue in originsOriginsitemsList) { xmlWriter.WriteStartElement("Origin", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); if (originsOriginsitemsListValue.IsSetId()) { xmlWriter.WriteElementString("Id", "http://cloudfront.amazonaws.com/doc/2013-08-26/", originsOriginsitemsListValue.Id.ToString()); } if (originsOriginsitemsListValue.IsSetDomainName()) { xmlWriter.WriteElementString("DomainName", "http://cloudfront.amazonaws.com/doc/2013-08-26/", originsOriginsitemsListValue.DomainName.ToString()); } if (originsOriginsitemsListValue != null) { S3OriginConfig s3OriginConfigS3OriginConfig = originsOriginsitemsListValue.S3OriginConfig; if (s3OriginConfigS3OriginConfig != null) { xmlWriter.WriteStartElement("S3OriginConfig", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); if (s3OriginConfigS3OriginConfig.IsSetOriginAccessIdentity()) { xmlWriter.WriteElementString("OriginAccessIdentity", "http://cloudfront.amazonaws.com/doc/2013-08-26/", s3OriginConfigS3OriginConfig.OriginAccessIdentity.ToString()); } xmlWriter.WriteEndElement(); } } if (originsOriginsitemsListValue != null) { CustomOriginConfig customOriginConfigCustomOriginConfig = originsOriginsitemsListValue.CustomOriginConfig; if (customOriginConfigCustomOriginConfig != null) { xmlWriter.WriteStartElement("CustomOriginConfig", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); if (customOriginConfigCustomOriginConfig.IsSetHTTPPort()) { xmlWriter.WriteElementString("HTTPPort", "http://cloudfront.amazonaws.com/doc/2013-08-26/", customOriginConfigCustomOriginConfig.HTTPPort.ToString()); } if (customOriginConfigCustomOriginConfig.IsSetHTTPSPort()) { xmlWriter.WriteElementString("HTTPSPort", "http://cloudfront.amazonaws.com/doc/2013-08-26/", customOriginConfigCustomOriginConfig.HTTPSPort.ToString()); } if (customOriginConfigCustomOriginConfig.IsSetOriginProtocolPolicy()) { xmlWriter.WriteElementString("OriginProtocolPolicy", "http://cloudfront.amazonaws.com/doc/2013-08-26/", customOriginConfigCustomOriginConfig.OriginProtocolPolicy.ToString()); } xmlWriter.WriteEndElement(); } } xmlWriter.WriteEndElement(); originsOriginsitemsListIndex++; } xmlWriter.WriteEndElement(); } } xmlWriter.WriteEndElement(); } } if (distributionConfigDistributionConfig != null) { DefaultCacheBehavior defaultCacheBehaviorDefaultCacheBehavior = distributionConfigDistributionConfig.DefaultCacheBehavior; if (defaultCacheBehaviorDefaultCacheBehavior != null) { xmlWriter.WriteStartElement("DefaultCacheBehavior", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); if (defaultCacheBehaviorDefaultCacheBehavior.IsSetTargetOriginId()) { xmlWriter.WriteElementString("TargetOriginId", "http://cloudfront.amazonaws.com/doc/2013-08-26/", defaultCacheBehaviorDefaultCacheBehavior.TargetOriginId.ToString()); } if (defaultCacheBehaviorDefaultCacheBehavior != null) { ForwardedValues forwardedValuesForwardedValues = defaultCacheBehaviorDefaultCacheBehavior.ForwardedValues; if (forwardedValuesForwardedValues != null) { xmlWriter.WriteStartElement("ForwardedValues", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); if (forwardedValuesForwardedValues.IsSetQueryString()) { xmlWriter.WriteElementString("QueryString", "http://cloudfront.amazonaws.com/doc/2013-08-26/", forwardedValuesForwardedValues.QueryString.ToString().ToLower()); } if (forwardedValuesForwardedValues != null) { CookiePreference cookiePreferenceCookies = forwardedValuesForwardedValues.Cookies; if (cookiePreferenceCookies != null) { xmlWriter.WriteStartElement("Cookies", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); if (cookiePreferenceCookies.IsSetForward()) { xmlWriter.WriteElementString("Forward", "http://cloudfront.amazonaws.com/doc/2013-08-26/", cookiePreferenceCookies.Forward.ToString()); } if (cookiePreferenceCookies != null) { CookieNames cookieNamesWhitelistedNames = cookiePreferenceCookies.WhitelistedNames; if (cookieNamesWhitelistedNames != null) { xmlWriter.WriteStartElement("WhitelistedNames", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); if (cookieNamesWhitelistedNames.IsSetQuantity()) { xmlWriter.WriteElementString("Quantity", "http://cloudfront.amazonaws.com/doc/2013-08-26/", cookieNamesWhitelistedNames.Quantity.ToString()); } if (cookieNamesWhitelistedNames != null) { List <string> cookieNamesWhitelistedNamesitemsList = cookieNamesWhitelistedNames.Items; if (cookieNamesWhitelistedNamesitemsList != null && cookieNamesWhitelistedNamesitemsList.Count > 0) { int cookieNamesWhitelistedNamesitemsListIndex = 1; xmlWriter.WriteStartElement("Items", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); foreach (string cookieNamesWhitelistedNamesitemsListValue in cookieNamesWhitelistedNamesitemsList) { xmlWriter.WriteStartElement("Name", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); xmlWriter.WriteValue(cookieNamesWhitelistedNamesitemsListValue); xmlWriter.WriteEndElement(); cookieNamesWhitelistedNamesitemsListIndex++; } xmlWriter.WriteEndElement(); } } xmlWriter.WriteEndElement(); } } xmlWriter.WriteEndElement(); } } xmlWriter.WriteEndElement(); } } if (defaultCacheBehaviorDefaultCacheBehavior != null) { TrustedSigners trustedSignersTrustedSigners = defaultCacheBehaviorDefaultCacheBehavior.TrustedSigners; if (trustedSignersTrustedSigners != null) { xmlWriter.WriteStartElement("TrustedSigners", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); if (trustedSignersTrustedSigners.IsSetEnabled()) { xmlWriter.WriteElementString("Enabled", "http://cloudfront.amazonaws.com/doc/2013-08-26/", trustedSignersTrustedSigners.Enabled.ToString().ToLower()); } if (trustedSignersTrustedSigners.IsSetQuantity()) { xmlWriter.WriteElementString("Quantity", "http://cloudfront.amazonaws.com/doc/2013-08-26/", trustedSignersTrustedSigners.Quantity.ToString()); } if (trustedSignersTrustedSigners != null) { List <string> trustedSignersTrustedSignersitemsList = trustedSignersTrustedSigners.Items; if (trustedSignersTrustedSignersitemsList != null && trustedSignersTrustedSignersitemsList.Count > 0) { int trustedSignersTrustedSignersitemsListIndex = 1; xmlWriter.WriteStartElement("Items", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); foreach (string trustedSignersTrustedSignersitemsListValue in trustedSignersTrustedSignersitemsList) { xmlWriter.WriteStartElement("AwsAccountNumber", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); xmlWriter.WriteValue(trustedSignersTrustedSignersitemsListValue); xmlWriter.WriteEndElement(); trustedSignersTrustedSignersitemsListIndex++; } xmlWriter.WriteEndElement(); } } xmlWriter.WriteEndElement(); } } if (defaultCacheBehaviorDefaultCacheBehavior.IsSetViewerProtocolPolicy()) { xmlWriter.WriteElementString("ViewerProtocolPolicy", "http://cloudfront.amazonaws.com/doc/2013-08-26/", defaultCacheBehaviorDefaultCacheBehavior.ViewerProtocolPolicy.ToString()); } if (defaultCacheBehaviorDefaultCacheBehavior.IsSetMinTTL()) { xmlWriter.WriteElementString("MinTTL", "http://cloudfront.amazonaws.com/doc/2013-08-26/", defaultCacheBehaviorDefaultCacheBehavior.MinTTL.ToString()); } xmlWriter.WriteEndElement(); } } if (distributionConfigDistributionConfig != null) { CacheBehaviors cacheBehaviorsCacheBehaviors = distributionConfigDistributionConfig.CacheBehaviors; if (cacheBehaviorsCacheBehaviors != null) { xmlWriter.WriteStartElement("CacheBehaviors", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); if (cacheBehaviorsCacheBehaviors.IsSetQuantity()) { xmlWriter.WriteElementString("Quantity", "http://cloudfront.amazonaws.com/doc/2013-08-26/", cacheBehaviorsCacheBehaviors.Quantity.ToString()); } if (cacheBehaviorsCacheBehaviors != null) { List <CacheBehavior> cacheBehaviorsCacheBehaviorsitemsList = cacheBehaviorsCacheBehaviors.Items; if (cacheBehaviorsCacheBehaviorsitemsList != null && cacheBehaviorsCacheBehaviorsitemsList.Count > 0) { int cacheBehaviorsCacheBehaviorsitemsListIndex = 1; xmlWriter.WriteStartElement("Items", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); foreach (CacheBehavior cacheBehaviorsCacheBehaviorsitemsListValue in cacheBehaviorsCacheBehaviorsitemsList) { xmlWriter.WriteStartElement("CacheBehavior", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); if (cacheBehaviorsCacheBehaviorsitemsListValue.IsSetPathPattern()) { xmlWriter.WriteElementString("PathPattern", "http://cloudfront.amazonaws.com/doc/2013-08-26/", cacheBehaviorsCacheBehaviorsitemsListValue.PathPattern.ToString()); } if (cacheBehaviorsCacheBehaviorsitemsListValue.IsSetTargetOriginId()) { xmlWriter.WriteElementString("TargetOriginId", "http://cloudfront.amazonaws.com/doc/2013-08-26/", cacheBehaviorsCacheBehaviorsitemsListValue.TargetOriginId.ToString()); } if (cacheBehaviorsCacheBehaviorsitemsListValue != null) { ForwardedValues forwardedValuesForwardedValues = cacheBehaviorsCacheBehaviorsitemsListValue.ForwardedValues; if (forwardedValuesForwardedValues != null) { xmlWriter.WriteStartElement("ForwardedValues", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); if (forwardedValuesForwardedValues.IsSetQueryString()) { xmlWriter.WriteElementString("QueryString", "http://cloudfront.amazonaws.com/doc/2013-08-26/", forwardedValuesForwardedValues.QueryString.ToString().ToLower()); } if (forwardedValuesForwardedValues != null) { CookiePreference cookiePreferenceCookies = forwardedValuesForwardedValues.Cookies; if (cookiePreferenceCookies != null) { xmlWriter.WriteStartElement("Cookies", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); if (cookiePreferenceCookies.IsSetForward()) { xmlWriter.WriteElementString("Forward", "http://cloudfront.amazonaws.com/doc/2013-08-26/", cookiePreferenceCookies.Forward.ToString()); } if (cookiePreferenceCookies != null) { CookieNames cookieNamesWhitelistedNames = cookiePreferenceCookies.WhitelistedNames; if (cookieNamesWhitelistedNames != null) { xmlWriter.WriteStartElement("WhitelistedNames", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); if (cookieNamesWhitelistedNames.IsSetQuantity()) { xmlWriter.WriteElementString("Quantity", "http://cloudfront.amazonaws.com/doc/2013-08-26/", cookieNamesWhitelistedNames.Quantity.ToString()); } if (cookieNamesWhitelistedNames != null) { List <string> cookieNamesWhitelistedNamesitemsList = cookieNamesWhitelistedNames.Items; if (cookieNamesWhitelistedNamesitemsList != null && cookieNamesWhitelistedNamesitemsList.Count > 0) { int cookieNamesWhitelistedNamesitemsListIndex = 1; xmlWriter.WriteStartElement("Items", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); foreach (string cookieNamesWhitelistedNamesitemsListValue in cookieNamesWhitelistedNamesitemsList) { xmlWriter.WriteStartElement("Name", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); xmlWriter.WriteValue(cookieNamesWhitelistedNamesitemsListValue); xmlWriter.WriteEndElement(); cookieNamesWhitelistedNamesitemsListIndex++; } xmlWriter.WriteEndElement(); } } xmlWriter.WriteEndElement(); } } xmlWriter.WriteEndElement(); } } xmlWriter.WriteEndElement(); } } if (cacheBehaviorsCacheBehaviorsitemsListValue != null) { TrustedSigners trustedSignersTrustedSigners = cacheBehaviorsCacheBehaviorsitemsListValue.TrustedSigners; if (trustedSignersTrustedSigners != null) { xmlWriter.WriteStartElement("TrustedSigners", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); if (trustedSignersTrustedSigners.IsSetEnabled()) { xmlWriter.WriteElementString("Enabled", "http://cloudfront.amazonaws.com/doc/2013-08-26/", trustedSignersTrustedSigners.Enabled.ToString().ToLower()); } if (trustedSignersTrustedSigners.IsSetQuantity()) { xmlWriter.WriteElementString("Quantity", "http://cloudfront.amazonaws.com/doc/2013-08-26/", trustedSignersTrustedSigners.Quantity.ToString()); } if (trustedSignersTrustedSigners != null) { List <string> trustedSignersTrustedSignersitemsList = trustedSignersTrustedSigners.Items; if (trustedSignersTrustedSignersitemsList != null && trustedSignersTrustedSignersitemsList.Count > 0) { int trustedSignersTrustedSignersitemsListIndex = 1; xmlWriter.WriteStartElement("Items", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); foreach (string trustedSignersTrustedSignersitemsListValue in trustedSignersTrustedSignersitemsList) { xmlWriter.WriteStartElement("AwsAccountNumber", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); xmlWriter.WriteValue(trustedSignersTrustedSignersitemsListValue); xmlWriter.WriteEndElement(); trustedSignersTrustedSignersitemsListIndex++; } xmlWriter.WriteEndElement(); } } xmlWriter.WriteEndElement(); } } if (cacheBehaviorsCacheBehaviorsitemsListValue.IsSetViewerProtocolPolicy()) { xmlWriter.WriteElementString("ViewerProtocolPolicy", "http://cloudfront.amazonaws.com/doc/2013-08-26/", cacheBehaviorsCacheBehaviorsitemsListValue.ViewerProtocolPolicy.ToString()); } if (cacheBehaviorsCacheBehaviorsitemsListValue.IsSetMinTTL()) { xmlWriter.WriteElementString("MinTTL", "http://cloudfront.amazonaws.com/doc/2013-08-26/", cacheBehaviorsCacheBehaviorsitemsListValue.MinTTL.ToString()); } xmlWriter.WriteEndElement(); cacheBehaviorsCacheBehaviorsitemsListIndex++; } xmlWriter.WriteEndElement(); } } xmlWriter.WriteEndElement(); } } if (distributionConfigDistributionConfig != null) { CustomErrorResponses customErrorResponsesCustomErrorResponses = distributionConfigDistributionConfig.CustomErrorResponses; if (customErrorResponsesCustomErrorResponses != null) { xmlWriter.WriteStartElement("CustomErrorResponses", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); if (customErrorResponsesCustomErrorResponses.IsSetQuantity()) { xmlWriter.WriteElementString("Quantity", "http://cloudfront.amazonaws.com/doc/2013-08-26/", customErrorResponsesCustomErrorResponses.Quantity.ToString()); } if (customErrorResponsesCustomErrorResponses != null) { List <CustomErrorResponse> customErrorResponsesCustomErrorResponsesitemsList = customErrorResponsesCustomErrorResponses.Items; if (customErrorResponsesCustomErrorResponsesitemsList != null && customErrorResponsesCustomErrorResponsesitemsList.Count > 0) { int customErrorResponsesCustomErrorResponsesitemsListIndex = 1; xmlWriter.WriteStartElement("Items", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); foreach (CustomErrorResponse customErrorResponsesCustomErrorResponsesitemsListValue in customErrorResponsesCustomErrorResponsesitemsList) { xmlWriter.WriteStartElement("CustomErrorResponse", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); if (customErrorResponsesCustomErrorResponsesitemsListValue.IsSetErrorCode()) { xmlWriter.WriteElementString("ErrorCode", "http://cloudfront.amazonaws.com/doc/2013-08-26/", customErrorResponsesCustomErrorResponsesitemsListValue.ErrorCode.ToString()); } if (customErrorResponsesCustomErrorResponsesitemsListValue.IsSetResponsePagePath()) { xmlWriter.WriteElementString("ResponsePagePath", "http://cloudfront.amazonaws.com/doc/2013-08-26/", customErrorResponsesCustomErrorResponsesitemsListValue.ResponsePagePath.ToString()); } if (customErrorResponsesCustomErrorResponsesitemsListValue.IsSetResponseCode()) { xmlWriter.WriteElementString("ResponseCode", "http://cloudfront.amazonaws.com/doc/2013-08-26/", customErrorResponsesCustomErrorResponsesitemsListValue.ResponseCode.ToString()); } if (customErrorResponsesCustomErrorResponsesitemsListValue.IsSetErrorCachingMinTTL()) { xmlWriter.WriteElementString("ErrorCachingMinTTL", "http://cloudfront.amazonaws.com/doc/2013-08-26/", customErrorResponsesCustomErrorResponsesitemsListValue.ErrorCachingMinTTL.ToString()); } xmlWriter.WriteEndElement(); customErrorResponsesCustomErrorResponsesitemsListIndex++; } xmlWriter.WriteEndElement(); } } xmlWriter.WriteEndElement(); } } if (distributionConfigDistributionConfig.IsSetComment()) { xmlWriter.WriteElementString("Comment", "http://cloudfront.amazonaws.com/doc/2013-08-26/", distributionConfigDistributionConfig.Comment.ToString()); } if (distributionConfigDistributionConfig != null) { LoggingConfig loggingConfigLogging = distributionConfigDistributionConfig.Logging; if (loggingConfigLogging != null) { xmlWriter.WriteStartElement("Logging", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); if (loggingConfigLogging.IsSetEnabled()) { xmlWriter.WriteElementString("Enabled", "http://cloudfront.amazonaws.com/doc/2013-08-26/", loggingConfigLogging.Enabled.ToString().ToLower()); } if (loggingConfigLogging.IsSetIncludeCookies()) { xmlWriter.WriteElementString("IncludeCookies", "http://cloudfront.amazonaws.com/doc/2013-08-26/", loggingConfigLogging.IncludeCookies.ToString().ToLower()); } if (loggingConfigLogging.IsSetBucket()) { xmlWriter.WriteElementString("Bucket", "http://cloudfront.amazonaws.com/doc/2013-08-26/", loggingConfigLogging.Bucket.ToString()); } if (loggingConfigLogging.IsSetPrefix()) { xmlWriter.WriteElementString("Prefix", "http://cloudfront.amazonaws.com/doc/2013-08-26/", loggingConfigLogging.Prefix.ToString()); } xmlWriter.WriteEndElement(); } } if (distributionConfigDistributionConfig.IsSetPriceClass()) { xmlWriter.WriteElementString("PriceClass", "http://cloudfront.amazonaws.com/doc/2013-08-26/", distributionConfigDistributionConfig.PriceClass.ToString()); } if (distributionConfigDistributionConfig.IsSetEnabled()) { xmlWriter.WriteElementString("Enabled", "http://cloudfront.amazonaws.com/doc/2013-08-26/", distributionConfigDistributionConfig.Enabled.ToString().ToLower()); } if (distributionConfigDistributionConfig != null) { ViewerCertificate viewerCertificateViewerCertificate = distributionConfigDistributionConfig.ViewerCertificate; if (viewerCertificateViewerCertificate != null) { xmlWriter.WriteStartElement("ViewerCertificate", "http://cloudfront.amazonaws.com/doc/2013-08-26/"); if (viewerCertificateViewerCertificate.IsSetIAMCertificateId()) { xmlWriter.WriteElementString("IAMCertificateId", "http://cloudfront.amazonaws.com/doc/2013-08-26/", viewerCertificateViewerCertificate.IAMCertificateId.ToString()); } if (viewerCertificateViewerCertificate.IsSetCloudFrontDefaultCertificate()) { xmlWriter.WriteElementString("CloudFrontDefaultCertificate", "http://cloudfront.amazonaws.com/doc/2013-08-26/", viewerCertificateViewerCertificate.CloudFrontDefaultCertificate.ToString().ToLower()); } xmlWriter.WriteEndElement(); } } xmlWriter.WriteEndElement(); } } try { request.Content = System.Text.Encoding.UTF8.GetBytes(stringWriter.ToString()); request.Headers.Add("Content-Type", "application/xml"); } catch (EncoderFallbackException e) { throw new AmazonServiceException("Unable to marshall request to XML", e); } return(request); }
/// <summary> /// Allow only few updations to cloudfront like SSL /// </summary> /// <param name="distributionId"></param> /// <param name="etag"></param> /// <returns></returns> public static UpdateDistributionResponse UpdateCloudFront(string distributionId, string etag, ViewerCertificate viewerCertificate = null) { try { if (String.IsNullOrEmpty(distributionId) || String.IsNullOrEmpty(etag)) { return(null); } var client = new AmazonCloudFrontClient(RegionEndpoint.APSouth1); var request = new UpdateDistributionRequest() { Id = distributionId, IfMatch = etag, DistributionConfig = new DistributionConfig() { } }; if (viewerCertificate != null) { request.DistributionConfig.ViewerCertificate = viewerCertificate; } return(client.UpdateDistributionAsync(request).Result); } catch (Exception ex) { //TODO - log the exception } return(null); }