Skip to content

Commit

Permalink
Azure Go Container Apps (pulumi#1109)
Browse files Browse the repository at this point in the history
* Azure Go Container Apps

* Simplify
  • Loading branch information
mikhailshilkov committed Nov 3, 2021
1 parent 7fd20c4 commit 399e497
Show file tree
Hide file tree
Showing 11 changed files with 612 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ Example | Description |

Example | Description |
--------- | --------- |
[Azure Container Apps](azure-ts-containerapps) | Run a Docker image on Azure Container Apps.
[Azure Container Instance](azure-ts-aci) | Run Azure Container Instances on Linux.
[Azure Kubernetes Service](azure-ts-aks) | Create an Azure Kubernetes Service (AKS) Cluster.
[Azure App Service](azure-ts-appservice) | Build a web application hosted in App Service and provision Azure SQL Database and Azure Application Insights.
Expand All @@ -173,6 +174,7 @@ Example | Description |

Example | Description |
--------- | --------- |
[Azure Container Apps](azure-py-containerapps) | Run a Docker image on Azure Container Apps.
[Azure Container Instance](azure-py-aci) | Run Azure Container Instances on Linux.
[Azure Kubernetes Service](azure-py-aks) | Create an Azure Kubernetes Service (AKS) Cluster.
[Azure App Service](azure-py-appservice) | Build a web application hosted in App Service and provision Azure SQL Database and Azure Application Insights.
Expand All @@ -189,6 +191,7 @@ Example | Description |

Example | Description |
--------- | --------- |
[Azure Container Apps](azure-go-containerapps) | Run a Docker image on Azure Container Apps.
[Azure Container Instance](azure-go-aci) | Run Azure Container Instances on Linux.
[Azure Kubernetes Service](azure-go-aks) | Create an Azure Kubernetes Service (AKS) Cluster.
[Azure App Service with Docker](azure-go-appservice-docker) | Build a web application hosted in App Service from Docker images.
Expand All @@ -200,6 +203,7 @@ Example | Description |
Example | Description |
--------- | --------- |
Cluster.
[Azure Container Apps](azure-cs-containerapps) | Run a Docker image on Azure Container Apps.
[Azure Container Instance](azure-cs-aci) | Run Azure Container Instances on Linux.
[Azure Kubernetes Service](azure-cs-aks) | Create an Azure Kubernetes Service (AKS) Cluster.
[AKS web app with .NET 5](azure-cs-net5-aks-webapp) | Create an Azure Kubernetes Service (AKS) cluster and deploy a web app to it using .NET 5 and C# 9.
Expand Down
3 changes: 3 additions & 0 deletions azure-go-containerapps/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: azure-go-containerapps
runtime: go
description: Creates an Azure Container App and deploys a custom Docker image to it
51 changes: 51 additions & 0 deletions azure-go-containerapps/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new)

# Azure Container Apps

Starting point for building web application hosted in Azure Container Apps.

## Running the App

1. Create a new stack:

```
$ pulumi stack init dev
```

1. Login to Azure CLI (you will be prompted to do this during deployment if you forget this step):

```
$ az login
```

1. Set the Azure region location to use:

```
$ pulumi config set azure-native:location westus2
```

1. Run `pulumi up` to preview and deploy changes:

```
$ pulumi up
Previewing changes:
...
Performing changes:
...
Resources:
+ 7 created
Duration: 4m18s
```

1. Check the deployed endpoint:

```
$ curl "$(pulumi stack output url)"
<html>
<body>
<h1>Your custom docker image is running in Azure Container Apps!</h1>
</body>
</html>
```
9 changes: 9 additions & 0 deletions azure-go-containerapps/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/pulumi/examples/azure-go-containerapps

go 1.16

require (
github.com/pulumi/pulumi-azure-native/sdk v1.44.0
github.com/pulumi/pulumi-docker/sdk/v3 v3.1.0
github.com/pulumi/pulumi/sdk/v3 v3.16.0
)
360 changes: 360 additions & 0 deletions azure-go-containerapps/go.sum

Large diffs are not rendered by default.

146 changes: 146 additions & 0 deletions azure-go-containerapps/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// Copyright 2016-2021, Pulumi Corporation. All rights reserved.
package main

import (
"github.com/pulumi/pulumi-azure-native/sdk/go/azure/containerregistry"
"github.com/pulumi/pulumi-azure-native/sdk/go/azure/operationalinsights"
"github.com/pulumi/pulumi-azure-native/sdk/go/azure/resources"
web "github.com/pulumi/pulumi-azure-native/sdk/go/azure/web/v20210301"
"github.com/pulumi/pulumi-docker/sdk/v3/go/docker"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func main() {
pulumi.Run(func(ctx *pulumi.Context) error {

resourceGroup, err := resources.NewResourceGroup(ctx, "rg", nil)
if err != nil {
return err
}

workspace, err := operationalinsights.NewWorkspace(ctx, "workspace", &operationalinsights.WorkspaceArgs{
ResourceGroupName: resourceGroup.Name,
RetentionInDays: pulumi.Int(30),
Sku: operationalinsights.WorkspaceSkuArgs{
Name: pulumi.String("PerGB2018"),
},
})
if err != nil {
return err
}

sharedKey := pulumi.All(resourceGroup.Name, workspace.Name).ApplyT(
func(args []interface{}) (string, error) {
resourceGroupName := args[0].(string)
workspaceName := args[1].(string)
accountKeys, err := operationalinsights.GetSharedKeys(ctx, &operationalinsights.GetSharedKeysArgs{
ResourceGroupName: resourceGroupName,
WorkspaceName: workspaceName,
})
if err != nil {
return "", err
}

return *accountKeys.PrimarySharedKey, nil
},
).(pulumi.StringOutput)

kubeEnvironment, err := web.NewKubeEnvironment(ctx, "kubeEnvironment", &web.KubeEnvironmentArgs{
ResourceGroupName: resourceGroup.Name,
Type: pulumi.String("Managed"),
AppLogsConfiguration: web.AppLogsConfigurationArgs{
Destination: pulumi.String("log-analytics"),
LogAnalyticsConfiguration: web.LogAnalyticsConfigurationArgs{
CustomerId: workspace.CustomerId,
SharedKey: sharedKey,
},
},
})
if err != nil {
return err
}

registry, err := containerregistry.NewRegistry(ctx, "registry", &containerregistry.RegistryArgs{
ResourceGroupName: resourceGroup.Name,
Sku: containerregistry.SkuArgs{
Name: pulumi.String("Basic"),
},
AdminUserEnabled: pulumi.Bool(true),
})
if err != nil {
return err
}
credentials := pulumi.All(resourceGroup.Name, registry.Name).ApplyT(
func(args []interface{}) (*containerregistry.ListRegistryCredentialsResult, error) {
resourceGroupName := args[0].(string)
registryName := args[1].(string)
return containerregistry.ListRegistryCredentials(ctx, &containerregistry.ListRegistryCredentialsArgs{
ResourceGroupName: resourceGroupName,
RegistryName: registryName,
})
},
)

adminUsername := credentials.ApplyT(func(result interface{}) (string, error) {
credentials := result.(*containerregistry.ListRegistryCredentialsResult)
return *credentials.Username, nil
}).(pulumi.StringOutput)
adminPassword := credentials.ApplyT(func(result interface{}) (string, error) {
credentials := result.(*containerregistry.ListRegistryCredentialsResult)
return *credentials.Passwords[0].Value, nil
}).(pulumi.StringOutput)

newImage, err := docker.NewImage(ctx, "node-app", &docker.ImageArgs{
ImageName: pulumi.Sprintf("https://%s/node-app:v1.0.0", registry.LoginServer),
Build: docker.DockerBuildArgs{
Context: pulumi.String("/node-app"),
},
Registry: docker.ImageRegistryArgs{
Server: registry.LoginServer,
Username: adminUsername,
Password: adminPassword,
},
})
if err != nil {
return err
}

containerApp, err := web.NewContainerApp(ctx, "app", &web.ContainerAppArgs{
ResourceGroupName: resourceGroup.Name,
KubeEnvironmentId: kubeEnvironment.ID(),
Configuration: web.ConfigurationArgs{
Ingress: web.IngressArgs{
External: pulumi.Bool(true),
TargetPort: pulumi.IntPtr(80),
},
Registries: web.RegistryCredentialsArray{
web.RegistryCredentialsArgs{
Server: registry.LoginServer,
Username: adminUsername,
PasswordSecretRef: pulumi.String("pwd")},
},
Secrets: web.SecretArray{
web.SecretArgs{
Name: pulumi.String("pwd"),
Value: adminPassword,
},
},
},
Template: web.TemplateArgs{
Containers: web.ContainerArray{
web.ContainerArgs{
Name: pulumi.String("myapp"),
Image: newImage.ImageName,
},
},
},
})
if err != nil {
return err
}

ctx.Export("url", pulumi.Sprintf("https://%s", containerApp.LatestRevisionFqdn))

return nil
})
}
2 changes: 2 additions & 0 deletions azure-go-containerapps/node-app/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
npm-debug.log
6 changes: 6 additions & 0 deletions azure-go-containerapps/node-app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM node:8.9.3-alpine
RUN mkdir -p /usr/src/app
COPY ./app/* /usr/src/app/
WORKDIR /usr/src/app
RUN npm install
CMD node /usr/src/app/index.js
6 changes: 6 additions & 0 deletions azure-go-containerapps/node-app/app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

<html>
<body>
<h1>Your custom docker image is running in Azure Container Apps!</h1>
</body>
</html>
13 changes: 13 additions & 0 deletions azure-go-containerapps/node-app/app/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const express = require('express');
const morgan = require('morgan');

const app = express();
app.use(morgan('combined'));

app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html')
});

var listener = app.listen(process.env.PORT || 80, function() {
console.log('listening on port ' + listener.address().port);
});
12 changes: 12 additions & 0 deletions azure-go-containerapps/node-app/app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "node-helloworld",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"express": "^4.14.0",
"morgan": "^1.8.2"
},
"devDependencies": {},
"author": ""
}

0 comments on commit 399e497

Please sign in to comment.