public MyStack() { // TODO: Remove after autonaming support is added. var randomSuffix = new RandomString("randomSuffix", new RandomStringArgs { Length = 10, Special = false, Upper = false, }); var config = new Config(); var storageAccountName = config.Get("storageAccountName") != null?Output.Create(config.Get("storageAccountName") !) : randomSuffix.Result.Apply(result => $"site{result}"); var cdnEndpointName = config.Get("cdnEndpointName") != null?Output.Create(config.Get("cdnEndpointName") !) : storageAccountName.Apply(result => $"cdn-endpnt-{result}"); var cdnProfileName = config.Get("cdnProfileName") != null?Output.Create(config.Get("cdnProfileName") !) : storageAccountName.Apply(result => $"cdn-profile-{result}"); var resourceGroup = new Resources.ResourceGroup("resourceGroup", new Resources.ResourceGroupArgs { ResourceGroupName = randomSuffix.Result.Apply(result => $"rg{result}"), }); var profile = new Cdn.Profile("profile", new Cdn.ProfileArgs { ProfileName = cdnProfileName, ResourceGroupName = resourceGroup.Name, Sku = new Cdn.Inputs.SkuArgs { Name = Cdn.SkuName.Standard_Microsoft, }, }); var storageAccount = new Storage.StorageAccount("storageAccount", new Storage.StorageAccountArgs { AccessTier = Storage.AccessTier.Hot, AccountName = storageAccountName, EnableHttpsTrafficOnly = true, Encryption = new Storage.Inputs.EncryptionArgs { KeySource = Storage.KeySource.Microsoft_Storage, Services = new Storage.Inputs.EncryptionServicesArgs { Blob = new Storage.Inputs.EncryptionServiceArgs { Enabled = true, }, File = new Storage.Inputs.EncryptionServiceArgs { Enabled = true, }, }, }, Kind = Storage.Kind.StorageV2, NetworkRuleSet = new Storage.Inputs.NetworkRuleSetArgs { Bypass = Storage.Bypass.AzureServices, DefaultAction = Storage.DefaultAction.Allow, }, ResourceGroupName = resourceGroup.Name, Sku = new Storage.Inputs.SkuArgs { Name = Storage.SkuName.Standard_LRS, }, }); var endpointOrigin = storageAccount.PrimaryEndpoints.Apply(pe => pe.Web.Replace("https://", "").Replace("/", "")); var endpoint = new Cdn.Endpoint("endpoint", new Cdn.EndpointArgs { ContentTypesToCompress = { }, EndpointName = cdnEndpointName, IsCompressionEnabled = false, IsHttpAllowed = false, IsHttpsAllowed = true, OriginHostHeader = endpointOrigin, Origins = { new Cdn.Inputs.DeepCreatedOriginArgs { HostName = endpointOrigin, HttpsPort = 443, Name = Output.Tuple(randomSuffix.Result, cdnEndpointName).Apply(t => $"{t.Item2}-origin-{t.Item1}"), }, }, ProfileName = profile.Name, QueryStringCachingBehavior = Cdn.QueryStringCachingBehavior.NotSet, ResourceGroupName = resourceGroup.Name, }); // Enable static website support var staticWebsite = new Storage.StorageAccountStaticWebsite("staticWebsite", new Storage.StorageAccountStaticWebsiteArgs { AccountName = storageAccount.Name, ResourceGroupName = resourceGroup.Name, IndexDocument = "index.html", Error404Document = "404.html", }); var index_html = new Storage.Blob("index_html", new Storage.BlobArgs { BlobName = "index.html", ResourceGroupName = resourceGroup.Name, AccountName = storageAccount.Name, ContainerName = staticWebsite.ContainerName, Type = Storage.BlobType.Block, Source = new FileAsset("./wwwroot/index.html"), ContentType = "text/html", }); var notfound_html = new Storage.Blob("notfound_html", new Storage.BlobArgs { BlobName = "404.html", ResourceGroupName = resourceGroup.Name, AccountName = storageAccount.Name, ContainerName = staticWebsite.ContainerName, Type = Storage.BlobType.Block, Source = new FileAsset("./wwwroot/404.html"), ContentType = "text/html", }); // Web endpoint to the website this.StaticEndpoint = storageAccount.PrimaryEndpoints.Apply(primaryEndpoints => primaryEndpoints.Web); // CDN endpoint to the website. // Allow it some time after the deployment to get ready. this.CdnEndpoint = endpoint.HostName.Apply(hostName => $"https://{hostName}"); }
public MyStack() { // Create an Azure Resource Group var resourceGroup = new Resources.ResourceGroup("resourceGroup", new Resources.ResourceGroupArgs { ResourceGroupName = "logicappdemo-rg", }); // Create an Azure resource (Storage Account) var storageAccount = new Storage.StorageAccount("storageAccount", new Storage.StorageAccountArgs { AccountName = "logicappdemosa21", ResourceGroupName = resourceGroup.Name, Sku = new Storage.Inputs.SkuArgs { Name = Storage.SkuName.Standard_LRS, }, Kind = Storage.Kind.StorageV2, }); // Cosmos DB Account var cosmosdbAccount = new DocumentDB.DatabaseAccount("cosmosdbAccount", new DocumentDB.DatabaseAccountArgs { AccountName = "logicappdemo-cdb", ResourceGroupName = resourceGroup.Name, DatabaseAccountOfferType = DocumentDB.DatabaseAccountOfferType.Standard, Locations = { new DocumentDB.Inputs.LocationArgs { LocationName = resourceGroup.Location, FailoverPriority = 0, }, }, ConsistencyPolicy = new DocumentDB.Inputs.ConsistencyPolicyArgs { DefaultConsistencyLevel = DocumentDB.DefaultConsistencyLevel.Session, }, }); // Cosmos DB Database var db = new DocumentDB.SqlResourceSqlDatabase("db", new DocumentDB.SqlResourceSqlDatabaseArgs { DatabaseName = "sqldb", ResourceGroupName = resourceGroup.Name, AccountName = cosmosdbAccount.Name, Resource = new DocumentDB.Inputs.SqlDatabaseResourceArgs { Id = "sqldb", }, }); // Cosmos DB SQL Container var dbContainer = new DocumentDB.SqlResourceSqlContainer("dbContainer", new DocumentDB.SqlResourceSqlContainerArgs { ContainerName = "container", ResourceGroupName = resourceGroup.Name, AccountName = cosmosdbAccount.Name, DatabaseName = db.Name, Resource = new DocumentDB.Inputs.SqlContainerResourceArgs { Id = "container", }, }); var accountKeys = Output.Tuple(cosmosdbAccount.Name, resourceGroup.Name).Apply(values => { var cosmosdbAccountName = values.Item1; var resourceGroupName = values.Item2; return(DocumentDB.ListDatabaseAccountKeys.InvokeAsync(new DocumentDB.ListDatabaseAccountKeysArgs { AccountName = cosmosdbAccountName, ResourceGroupName = resourceGroupName, })); }); var clientConfig = Output.Create(Authorization.GetClientConfig.InvokeAsync()); var apiId = Output.Tuple(clientConfig, resourceGroup.Location).Apply(values => { var clientConfig = values.Item1; var location = values.Item2; return($"/subscriptions/{clientConfig.SubscriptionId}/providers/Microsoft.Web/locations/{location}/managedApis/documentdb"); }); // API Connection to be used in a Logic App var connection = new Web.Connection("connection", new Web.ConnectionArgs { ConnectionName = "cosmosdbConnection", ResourceGroupName = resourceGroup.Name, Properties = new Web.Inputs.ApiConnectionDefinitionPropertiesArgs { DisplayName = "cosmosdb_connection", Api = new Web.Inputs.ApiReferenceArgs { Id = apiId, }, ParameterValues = { { "databaseAccount", cosmosdbAccount.Name }, { "accessKey", accountKeys.Apply(accountKeys => accountKeys.PrimaryMasterKey) }, }, }, }); // Logic App with an HTTP trigger and Cosmos DB action var workflow = new Logic.Workflow("workflow", new Logic.WorkflowArgs { WorkflowName = "httpToCosmos", ResourceGroupName = resourceGroup.Name, Definition = new Dictionary <string, object> { { "$schema", "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#" }, { "contentVersion", "1.0.0.0" }, { "parameters", new Dictionary <string, object> { { "$connections", new Dictionary <string, object> { { "defaultValue", new Dictionary <string, object>() }, { "type", "Object" }, } }, } }, { "triggers", new Dictionary <string, object> { { "Receive_post", new Dictionary <string, object> { { "type", "Request" }, { "kind", "Http" }, { "inputs", new Dictionary <string, object> { { "method", "POST" }, { "schema", new Dictionary <string, object> { { "properties", new Dictionary <string, object>() }, { "type", "object" }, } }, } }, } }, } }, { "actions", new Dictionary <string, object> { { "write_body", new Dictionary <string, object> { { "type", "ApiConnection" }, { "inputs", new Dictionary <string, object> { { "body", new Dictionary <string, object> { { "data", "@triggerBody()" }, { "id", "@utcNow()" }, } }, { "host", new Dictionary <string, object> { { "$connection", new Dictionary <string, object> { { "name", "@parameters('$connections')['documentdb']['connectionId']" }, } }, } }, { "method", "post" }, { "path", Output.Tuple(db.Name, dbContainer.Name).Apply(values => { var dbName = values.Item1; var dbContainerName = values.Item2; return($"/dbs/{dbName}/colls/{dbContainerName}/docs"); }) }, } }, } }, } }, }, Parameters = { { "$connections", new Logic.Inputs.WorkflowParameterArgs { Value = new Dictionary <string, object> { { "documentdb", new Dictionary <string, object> { { "connectionId", connection.Id }, { "connectionName", "logicapp-cosmosdb-connection" }, { "id", apiId }, } }, }, } }, }, }); var callbackUrls = Output.Tuple(resourceGroup.Name, workflow.Name).Apply(values => { var resourceGroupName = values.Item1; var workflowName = values.Item2; return(Logic.ListWorkflowTriggerCallbackUrl.InvokeAsync(new Logic.ListWorkflowTriggerCallbackUrlArgs { ResourceGroupName = resourceGroupName, WorkflowName = workflowName, TriggerName = "Receive_post", })); }); this.Endpoint = callbackUrls.Apply(callbackUrls => callbackUrls.Value); }