DEV Community

Audu Ephraim
Audu Ephraim

Posted on

Azure Resume Challenge Using Pulumi, Golang, and Azure Blob Storage

I undertook the resume challenge on Azure. More about the resume challenge can be found in the AWS resume challenge which can be found Here.

The whole process was almost the same as the AWS challenge except for a few differences, which I will be pointing out.

Setting up the environment

Setting up the environment remained consistent except for a few changes when starting the project, I used “pulumi new azure-go” for an Azure project

Creating the Infrastructure

Because in Azure, every resource belongs to a resource group and blob storages belong to storage groups, I had to create a resource group and a storage group, also I had to create a static website on an Azure storage account.

Since Azure Blob Storage is an object store similar to AWS S3, I need to handle each file in my website folder, which consists of JavaScript, jQuery, and CSS code, as an object. To do this, I'll need to traverse the folder and create a corresponding blob for each file.

I created a new file blobFolder.go and added the following

type BlobFolder struct {
   pulumi.ResourceState

   containerName pulumi.StringOutput `pulumi:"containerName"`
}

func NewBlobFolder(ctx *pulumi.Context, containerName string, siteDir string, args *FolderArgs) (*BlobFolder, error) {

   var resource BlobFolder
   // Stack exports
   err := ctx.RegisterComponentResource("pulumi:example:BlobFolder", containerName, &resource)
   if err != nil {
      return nil, err
   }
   //creating a resource group
   resourceGroup, err := resources.NewResourceGroup(ctx, "new-resource-group", nil)
   if err != nil {
      return nil, err
   }
   //creating an azure storage account
   account, err := storage.NewStorageAccount(ctx, "newstorage", &storage.StorageAccountArgs{
      ResourceGroupName: resourceGroup.Name,
      Sku: &storage.SkuArgs{
         Name: pulumi.String("Standard_LRS"),
      },
      Kind: pulumi.String("StorageV2"),
   })
   if err != nil {
      return nil, err
   }
   //creating a static website
   staticWebsite, err := storage.NewStorageAccountStaticWebsite(ctx, "staticWebsite", &storage.StorageAccountStaticWebsiteArgs{
      AccountName:       account.Name,
      ResourceGroupName: resourceGroup.Name,
      IndexDocument:     pulumi.String("index.html"),
   })
   if err != nil {
      return nil, err
   }

   // For each file in the directory, create a blob object
   err = filepath.Walk(siteDir, func(name string, info fs.FileInfo, err error) error {
      if err != nil {
         return err
      }
      if !info.IsDir() {
         rel, err := filepath.Rel(siteDir, name)
         if err != nil {
            return err
         }

         _, err = storage.NewBlob(ctx, rel, &storage.BlobArgs{
            ResourceGroupName: resourceGroup.Name,
            AccountName:       account.Name,
            ContainerName:     staticWebsite.ContainerName,
            Source:            pulumi.NewFileAsset(name),
            ContentType:       pulumi.String(mime.TypeByExtension(path.Ext(name))),
         })
         if err != nil {
            return err
         }
      }
      return nil
   })
   if err != nil {
      return nil, err
   }

   ctx.Export("staticWebsite", account.PrimaryEndpoints)

   return &resource, nil
}

type folderArgs struct {
}

type FolderArgs struct {
}

func (FolderArgs) ElementType() reflect.Type {

   return reflect.TypeOf((*folderArgs)(nil)).Elem()
}
Enter fullscreen mode Exit fullscreen mode

then in main.go

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

        f, err := NewBlobFolder(ctx, "resume-container", "./website", &FolderArgs{})
        if err != nil {
        }
        ctx.Export("bucketName", f.containerName)
        return nil
    })
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

This project provided valuable insights into how both cloud platforms, Azure and AWS, are different yet very similar. Over time, I plan to extend this project and explore using different infrastructures.

link to the github repo https://github.com/audu97/azure-resume-challenge.git

Top comments (0)