normalian blog

Let's talk about Microsoft Azure, ASP.NET and Java!

Store passwords and secrets securely by using App Service and Azure Key Vault on each environment without code changes

It was sometimes painful task to change settings, connection string and others depending on environment - production, QT, staging or development. It's also quite important to ensure to keep secret and security for others. As a solution for the case, you can significantly simplify to retrieve secrets from Azure Key Vault with Managed Identity even now. In this post, you can acquire knowledge how to completely separate settings and password from application codes by using App Service, Azure Key Vault and Managed Identity like below.
f:id:waritohutsu:20210429071157p:plain

From next section, you can setup single environment by using App Service and Key Vault first,

App Service by using Managed Identity and Azure Key Vault

In this section, you need to reminder how to setup Managed Identity and Azure Key Vault on App Service with single environment. First, create your App Service and Key Vault like below.
f:id:waritohutsu:20210429063541p:plain

Enable System assigned managed identity like here. And pick up Object ID for this managed identity.
f:id:waritohutsu:20210429063743p:plain

Visit your Key Vault and create "Secret" like here to retrieve from your apps on Azure App Service in later.
f:id:waritohutsu:20210429064043p:plain

Assign access policy to your managed identity on Azure App Service by following steps below.
f:id:waritohutsu:20210429064404p:plain

Select your managed identity by putting your object id on search box and choose it like below. Change Secret permissions as you need - I have changed only "Get" to allow only reading.
f:id:waritohutsu:20210429064522p:plain

Visit your Azure App Service and put environment settings as KEY_VAULT_URI as https://xxxxxxxxxxxx.vault.azure.net/ and ENV_NAME as production like below.
f:id:waritohutsu:20210429064844p:plain

Now, it's a time to write up your application code. Lunch your Visual Studio and import modules below, Azure.Identity and Azure.Security.KeyVault.Secrets, to utilize Managed Identity and Azure Key Vault.
f:id:waritohutsu:20210429065229p:plain

you can retrieve secret on your Key Vault from App Service.

using Azure;
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace daisamiappservice.Pages
{
    public class IndexModel : PageModel
    {
        private readonly ILogger<IndexModel> _logger;

        public IndexModel(ILogger<IndexModel> logger)
        {
            _logger = logger;
        }

        public async Task OnGet()
        {
            try
            {
                string uri = Environment.GetEnvironmentVariable("KEY_VAULT_URI");
                SecretClient client = new SecretClient(new Uri(uri), new DefaultAzureCredential());

                Response<KeyVaultSecret> secret = await client.GetSecretAsync("secret");

                ViewData["Secret"] = $"{secret.Value.Name} : {secret.Value.Value}";
            }
            catch (Exception exp)
            {
                ViewData["Error"] = $"Something went wrong: {exp.Message}";
            }
        }
    }
}

You can retrieve secret like here without putting any secret strings on your app.
f:id:waritohutsu:20210429065704p:plain

Enable App Service deployment slot and Managed Identity to separate secrets completely for each environment

App Service offers deployment slots feature without lunching up new App Service instances. You just need to create deployment slot on your Azure App Service and enable managed identity again on your deployment slot because it's a separated Azure resource with production env. Create new Azure Key Vault and put settings like here. Don't forget to enable Deployment slot setting to prevent swap settings.
f:id:waritohutsu:20210429070259p:plain

Now, you can deploy as single code for each environment. Deploy your app into deployment slot and swap it into production. You can confirm your settings like below.
f:id:waritohutsu:20210429070650p:plain

Finnally, you can store secrets within Azure Key Vault and retrieve them by using Managed Identity without any specific password on your application codes or settings.

Execute Azure Automation Runbooks with Managed Identity

In past, it was required to execute Runbooks on Azure Automation by using RunAsAccount, but it's still mandatory to renew a self-signed certificate - as far as I remember, it would be annual. This renewal sometimes causes issues because some folks are unfamiliar about this.
Now, Azure Automation has just started to support for Managed Identity. This enable you not to force the renewal, and you can simplify your Runbooks scripts not only the renewal. In this article, you can run through to setup a Runbook Retrieving running VMs on your subscription.

Steps retrieving running VMs on your subscription

Follow steps are below.

  1. Create your Azure Automation account
  2. Enable Managed Identity on your Azure Automation account and assign proper RBAC roles
  3. Import "Az.Accounts" and "Az.Compute" modules to execute Az PowerShell commands on your Runbooks
  4. Create a Runbook and put PowerShell scripts

Enable Managed Identity on your Azure Automation account

I believe we can skip "Create your Azure Automation account" because it's too trivial. It's quite simple to enable Managed Identity on Azure Automation. Visit your Azure Automation account and choose new item named "Identity" like below. Then, switch "Status" as "On" and save it.
f:id:waritohutsu:20210425031827p:plain

Next, click "Azure role assignments" to assign "Virtual Machine Contributor" role to retrieve Azure VMs.
f:id:waritohutsu:20210425031957p:plain

Choose "Scope" as you need and choose "Virtual Machine Contributor" role like below.
f:id:waritohutsu:20210425035403p:plain

Import "Az.Accounts" and "Az.Compute" modules to execute Az PowerShell commands on your Runbooks

Azure Automation accounts don't import Azure Az PowerShell modules as default at this April 2021. Choose "Modules" from left side menus, and click "Browse gallery" button like below.
f:id:waritohutsu:20210425035902p:plain

Put "Az" in search box, so you can find all Az modules like below. Az modules has dependencies for each others, so import "Az.Accounts" first, and then import "Az.Compute" as next.
f:id:waritohutsu:20210425040041p:plain

Create a Runbook and put PowerShell scripts

Create new Runbook on your Azure Automation account and put scripts below.

Connect-AzAccount -Identity
Get-AzVM -Status | Where-Object PowerState -Like "*Running*" | Format-Table -AutoSize

Execute your Runbook and then you can find output below on "Job" menu.
f:id:waritohutsu:20210425040407p:plain

シアトル内で引っ越した際の住所更新手続き+引っ越し費用

3月頭にもともと住んでいた Bellevue downtown から車で10分くらいのところに引っ越しました。もろもろあって 1bed で手狭になったので、2bed の部屋に引っ越そうと思ったというのがモチベーションでした。住所変更で事務手続きを対応したのは以下です。渡米前は「SSN があるからそこに連動して住所も全部紐づいてるんだろうなぁ」とか思ってましたがあんなの幻想で、色々と申請しないといけないのは日本とあんま変わらないです。


原則は全部インターネット経由でできるので、そこではあんまり困ったりすることはなかったです。インターネットは移行時に繋げない時間があると仕事にならないレベルだったのでちょっと心配だったのですが、オンライン越しに事前に新居での利用日を申請し、元々使っているルーターを新居に運んで回線のコネクタに繋げたら普通につながりました(実は当初の移動日より1日遅れて移動したんですが、前の家でも普通に使えてて米国らしいゆるさを感じてました)。

あと必須なのが USPS(米国版の郵政)での郵便物の転送届ですが、Official USPS Change of Address Form からできます。これやってると最大1年?は古い住所の書類が新しい住所に転送されるので、都度気付いた毎に対応できます。

ちなみに私の場合は U-HAUL で大き目の車を借りて自分で引っ越ししました。机やベッドが運ぶのが大変でしたが、元々荷物は多くないので半日くらいかけて何度か新居と旧居の往復で終わらせたか感じです。いくら位かかったか気になる方もいると思うので以下に載せますが、車自体を借りるお金+走った距離で課金されることに加え、私の場合はカートを借りたのでその値段が入ってます。
f:id:waritohutsu:20210411055130p:plain

誰の得になるか知りませんが、ご参考まで。

渡米3年たったので英語絡みの雑感アレコレ

明けましておめでとうございます。渡米してそろそろ3年ほど経った&英語がらみのことを質問されることが非常に増えたので、所感を交えつつで自分の経験でも共有したら役に立つ人でもいるのかなと思ったのでちょっとまとめてみました。ご参考になれば幸いです。

あんた誰?

この節が必要な人は多分見に来ない気がしますが、シアトル本社のソフトウェア会社に勤めてます。渡米して3年半弱が過ぎ、1年半程がロサンゼルス、2年程がシアトルで働いてます。近隣のチームには原則日本人は全くおらず、一緒に働いている別会社側にも話す相手に日本人は原則いないので、普段の仕事はほぼ 100% 英語です。Role Title はソリューションアーキテクトなので、ソフトウェアディベロッパーよりも技術的に求められるレベルは低いと思いますが、求められる英語力はかなり高めになると思います(最終的には個々人に依ると思いますが。
追記そこそこ読んで頂いているっぽいので渡米前の英語力を言うと、新入社員時は TOEIC 400 点台を取ったことがあり、渡米直前ですら700点台でした。留学・海外在住経験は皆無です。

英語圏での仕事はどう?

かなり大変どころか、事前にこんなに大変だとちゃんと分かってたら渡米しなかったかも知れないレベルです。日本に居る場合は「英語ができる」が大きな武器になる事が多いですが、米国に居る場合は当然何のプラスにもならない挙句、英語ができない分だけただ不利になるので、常に巨大なハンデを背負って仕事をしている感覚が付きまといます。米国やヨーロッパ圏の人間と話をしている限り「英語でコミュニケーションすらおぼつかない民族がいる」という認識を持っている人間は原則居ない印象を受けています(APAC 圏の英語が不得意な国とコミュニケーションとったことのある奴らは別)。いまでこそ、そこそこの英会話はできるようになりましたが、渡米直後の2年程は常時巨大なプレッシャーを感じてました(米国側で働いてるとレイオフとか常時あるんで。
今の役職自体は色んな国の人間とやり取りすることが非常に多いので、幅広な知見や各国(米国はもちろんヨーロッパ圏、APAC 圏等々)の実案件に係るのは見分が広まって面白いです。

英語は慣れた?

これが一番よく聞かれる質問です。3年ほど経ちましたが全然慣れてません、未だに苦渋ばっか舐めてます。英語には慣れてませんが「英語が起因のつらい状況でどうやって仕事を遂行するか?」には慣れましたし、そういう能力は相当磨かれた気がします。自分の英語がイマイチな状況で「英語でうまく話そうと頑張る」とか実質的に無意味・無価値なので「想定される問答に対して如何に回答文章を考えておくか」や「説明が複雑になりそうな箇所を図等で説明しやすくしておく」等々の方が大事だと思いますし、そうした「英語以外の箇所で補完する」能力は大幅に向上しましたと思います。
私が今の時点で困ってる英語は「他社の CTO と戦略的エンゲージメントをどう握るか?」とか「グローバルで重要度の高い実案件に対して限られたリソースの中でどう対応していくか?」みたいな話なので、意思の疎通レベルについては特に困らない状況にはなったので、そこそこ成長はしていると思います。
英語のコミュニケーション力がぐっと向上するタイミングは「英語でよく話す相手が見つかる」だなと思うことが多く、ロサンゼルス時代は私の技術力に信を置いてくれて頻繁に話すようになったお客さん(Korean のナイスガイ)、シアトル時代は近くの席に座ってブラックジョークを言い合うインド人同僚とかは大変世話になりました。英語のコミュニケーションは慣れの要素が非常に大きいので、こういう相手の数をどう増やすかはかなり大事だと思ってますが、この新型コロナの影響でくだらない雑談の数が減ったのが非常に厳しいところです。

英語ってどうやって勉強したの?

これも非常に良く聞かれるんですが、①語彙の増強、②会話自体への慣れ、③発音矯正以外はないと思います。
①語彙の増強については単に勉強に対するやる気の問題なので割愛しますが、②を実践しない人が非常に多い印象です。英語で意思疎通すら成り立たないレベルな語学力だった当時、手始めとして自由に時間が取れるRarejob なりのオンライン英会話が結構良かったです。今は知りませんが、当時の Rarejob は相手がフィリピン人の方で English native ではありませんでしたが、少なくとも会話が成り立たない当時はここから初めるのは悪くなかったです。ただし、フィリピン人の方も上記の通り原則は English native ではないので、渡米時点で Rarejob でスムーズにコミュニケーション取れるレベルでもロサンゼルスでビジネス英会話は全然聞き取れないし、自分の発音は伝わらなくて本気で死にかけたので、過度な自信を持つのは危険です。1年半ほど居たロサンゼルスでは特定の相手と何度もコミュニケーションを取ったので、相手が私のダメ英語に慣れてきたのもあり後半はそこそこ英語で意思の疎通はできましたが、純技術的な話題だけでビジネス的な色が出た時点ではまだまだな感じでした。
③の発音矯正はシアトル移住後、普段からお世話になってる日本人の方に紹介してもらった方にトレーニングしてもらいましたが、大幅に発音の質が改善したと思います。オンラインだろうが聞き返されることは無くなった&新しくチームに入ったノルウェーのインド人からも「お前の英語は word by word で聞き取れるよ」と言われたりしたので、かなり発音が良くなったと思います。発音が改善されるとヒアリング力も向上するので、打合せに参加している際にも内職しつつでヒアリングはそこそこできるようになったのですが、たまに話題を振られて焦ったりするのは今もやってます(汗

日本人的にどの国の人の英語が聞き取りにくいとかある?

良く「インド人英語は聞きにくい」という話を聞きますが、周りにインド人の同僚が多すぎるせいかインド人英語は一言一句で聞き取れるレベルにはなりました(今となってはもはや聞きやすいレベル)。それ以外にもイギリス以外のヨーロッパ圏の相手なら原則一言一句レベルではヒアリングできていると思います。
一番厄介なのがイギリス人や生まれも育ちも米国な English native 勢です。上記のインド人、イギリス以外のヨーロッパ勢は英語が native 言語ではないので凝った言い回し等は原則しないですが、English native 勢で特に若めなタイプは会話のスピードが速かったり表現が native 特有とかの自覚がないので、まくしたてられると結構きついことが多いです。10人の打合せで3人が English native、4人がインド人、3人がアジア系とかだったりする場合、English native 勢ばっかしゃべってる打合せとかはちょいちょい見ます。
あと地味に厄介なのがオーストラリア・ニュージーランド以外の APAC 圏との会話です。日本人と同じく彼らも英語が得意ではない人が多いので、初めて打合せする様な場合だと発音的な意味で聞き取れないことが結構あります(何度か打合せするとお互い慣れるので大丈夫)。

英語のプレゼンとか大変でしょ?

渡米2年間くらいは涙が出るほど大変で、日本語で準備する労力が100としたら、英語で準備する労力が500位な感じでした。やり始めた当初はしゃべる内容の一言一句を文字起こしして事前に何度も練習してました。シアトルに移住後(渡米1年半後)は約二週に一度でプレゼンをしてますが、最初の半年くらいは文字起こしの上に何度もプレゼンの練習をしていましたので、かなりきつかったです(汗
今は文字起こしは全くやらずに事前に2~3回くらい試しで話す位ですし、英語で準備する労力は 150 位の労力な感じだと思います。

渡米して米国企業に勤めるのってどう?

ソフトウェアエンジニアとしてなら余裕でありだと思います。特に独身で若いなら、数年の渡米・米国本社企業勤務経験は肩書的にも有効なのでアリだと思います(数年後のことは渡米後に考えても年齢的にも全然遅くないし)。一概にお勧めできなくなるのが「①家族が居る場合」と「②ソフトウェアエンジニア以外で渡米する場合」です。
①は純粋に家族が米国になじめるかが巨大な壁になるからです。本人は覚悟をもって渡米しているから友人や仕事での人間関係がゼロリセットになっても耐えられると思いますが、家族はその限りではありません。また「子供は英語にすぐ慣れる」とよく言いますし当然嘘ではないですが、やはりどこの話を聞いても渡米直後は子供も英語で苦労しているので、この観点は非常に注意した方が良いです。
このブログ読む人間に②に該当するタイプはあまりいないと思いますが、この辺りになると英語でのコミュニケーション能力はかなり比重が高まってきます。営業側やマーケティング側に近くなればなるほど求められるコミュニケーション能力が激増していきますが、ここに non native な人間が真っ向から張り合うのはあまりお勧めできないです。

大分海外勢とコミュニケーションはできるようになったけど渡米できるかな?

上記でも書きましたが、ソフトウェアエンジニアとして渡米するならおそらく問題ないと思います(自分がソフトウェアエンジニアとして勤務してないので想像ですが)。ただ、そもそもで「日本国内にわざわざコンタクトをしてくる海外勢」は日本人が英語が非常に苦手なのを理解しているタイプが多いです。日本人に限らずアジア圏の人間の英語発音は総じてヨーロッパ圏・米国側の人間には非常に聞き取りずらいらしく、日本人のダメ発音になれてくれている海外勢とコミュニケーションが取れるからと気軽に渡米すると米国の English native に瞬殺される恐れがあるので要注意です(私は渡米直後に English native 勢に全然聞き取ってもらえなくて死にました。
特に米国に居ようがインド人の場合、彼・彼女ら自身が English non native かつアジア圏側の人間なので、インド発音の癖さえ慣れればかなり日本人的にコミュニケーションが取りやすい部類になると思います(渡米しているインド人も当然移民なので、体感の話ですが状況が似てるので相談しやすい良い人が多いです)。
もし米国やイギリスに出張する機会があれば、グローバル側の人間でなくローカル(米国内、イギリス内の意味)の人間とコミュニケーションを取ってみるのをお勧めします。求められる英語の水準が段違いになるのでかなり衝撃を受けると思います。

How to enable Azure Arc command line

Microsoft has offered Azure Arc to manage your Windows and Linux machines hosted outside of Azure, on your corporate network, or other cloud provider consistent with how you manage native Azure virtual machines. Its command lines have been released recently. Please note it's still under preview at 12/6/2020.
Command lines for Azure Arc has not been included into Azure Cli yet, so you need to execute commands below.

az extension add --name connectedmachine

It takes about 30sec, like below, and you can try the commands right after that.
f:id:waritohutsu:20201207000110p:plain

Completely turn off your AKS clusters to reduce your cost!

As you might know, it was not possible to stop your AKS clusters completely because system pools are always required to be running. I have posted about this like below in past.
normalian.hatenablog.com

Here is quite useful feature to reduce AKS cost much effectively than before.
docs.microsoft.com

It should be really easy to follow the article. I have open Azure Portal and try to stop my AKS cluster like below, and it seems to be fine even if you have never registered "extension aks-preview" like below.
f:id:waritohutsu:20201007120040p:plain

You can find your node pools will be 0 nodes on Azure Portal or command lines after you have completed the commands.
f:id:waritohutsu:20201007120440p:plain

Let's utlize Azure Front Door to route requests globally

Azure Front Door is useful feature to manage and monitor your web traffics with global routing. Azure Front Door enables you to manage and optimize your global(multi-regions) customers easily. I believe readers of my blog want to acquire practical knowledge, so let's start to try!

At first, you need to note that Azure Front Door is global resource as you can confirm on Azure Portal below. This means you're no longer to be bothered by regional perspective at least for Azure Front Door.
f:id:waritohutsu:20200705041348p:plain

What's resources we can setup on Azure Front Door?

Azure Front Door can choose various types of resources like below. You are also possible to route requests out of Azure Platform with "Custom Host" and setup FQDNs. In this post, you can acquire knowledge how to use "Storage", "Public IP Address" and "Custom Host"
f:id:waritohutsu:20200705045658p:plain

Setup a sample scenarios

Here is an example architecture which I have setup as a sample for Azure Front Door.
f:id:waritohutsu:20200705050415p:plain

After creating your Azure Front Door instance, choose "Front Door designer" and add your owned domain on Azure Portal like below.
f:id:waritohutsu:20200705050628p:plain

Next, you can add your backend resources on "Backend pools" menu like below.
f:id:waritohutsu:20200705050804p:plain

You can add "Microsoft Cloud Workshop" site like below by setting up as "Custom host".
f:id:waritohutsu:20200705050950p:plain

Finally, you can setup rules how to forward or redirect HTTP/HTTPS requests into backend pools. In this example, all requests match with "/storage01/*" will be forwarded to my Azure Storage account into "/" path. Don't forget to specify as "/storage01/*" not ""/storage01/"
f:id:waritohutsu:20200705051353p:plain

Access a sample with Azure Front Door

You can confirm each request routings like below.
f:id:waritohutsu:20200705052118p:plain