WindowsでAWS Toolkit for Visual Studio Code/PyCharmを使ってもサーバーレスアプリケーションを作成することができない ~ Error: Please verify your location. The following types of location are supported

これ、色々調べてみたり、試してみたが結局解決していない。

AWS Toolkitどうこういう前に、samコマンドを使ってCLIで試してみたが失敗。

PS C:\aws> sam init --runtime python3.7

Which template source would you like to use?
        1 - AWS Quick Start Templates
        2 - Custom Template Location
Choice: 1

Project name [sam-app]: 
Cloning app templates from https://github.com/awslabs/aws-sam-cli-app-templates.git

-----------------------
Generating application:
-----------------------
Name: sam-app
Runtime: python3.7
Dependency Manager: pip
Application Template: hello-world
Output Directory: .

Next steps can be found in the README file at ./sam-app/README.md

Error: Please verify your location. The following types of location are supported:

* Github: gh:user/repo (or) https://github.com/user/repo (or) git@github.com:user/repo.git
          For Git repositories, you must use location of the root of the repository.
* Mercurial: hg+ssh://hg@bitbucket.org/repo
* Http(s): https://example.com/code.zip
* Local Path: /path/to/code.zip

ただ、ググっても失敗した事例が見つからないので、AWS EC2(Linux)でsamコマンドを使って試してみたら、サンプルプロジェクトが作成された。
意味がわからない。

これはどういう意味なのか・・・。

Error: Please verify your location. The following types of location are supported:

Github: gh:user/repo (or) https://github.com/user/repo (or) git@github.com:user/repo.git

For Git repositories, you must use location of the root of the repository.

Mercurial: hg+ssh://hg@bitbucket.org/repo

Local Path: /path/to/code.zip

AWS S3のmount ~ goofysのインストール

s3fsは遅い!これは有名な話しかと思うので本題じゃなくて、新しいEC2環境にgoofysをインストールして使おうとしたときの話し。

ぐぐると、どのblogでも大体次のような流れが紹介されている。

1.goとfuseのインストール
2.AWS CLIの設定
3.S3バケットの作成
4.goofysインストール
5.S3をgoofysを使ってマウント

今回、分からなかったのが「4.goofysインストール」。
インストール自体は出来たようだが、goofysのコマンドが見つからない
だいたい、5の手順で「$GOPATH/bin/goofys your-s3-bucket-name /mnt/your-s3-bucket-name」を実行するとか書かれているが、そもそも「locate bin/goofys」でも検索結果0だったし・・・。
どこにインストールされてんのや???????

結局、次のようにしてwgetで持ってきてインストール。

$cd /usr/local/bin/
$sudo wget https://github.com/kahing/goofys/releases/latest/download/goofys
$sudo chmod +x goofys

最後にマウントできるか確認。
$goofys S3バケット名 mount用ディレクトリ名

AWS ~ Python3.7のインストールでエラー「ModuleNotFoundError: No module named '_ctypes'」。libffi-devをインストールして解決。

めんど!
インストールエラー。。。

[root@xxxx dir]# pyenv install 3.7.0
Downloading Python-3.7.0.tar.xz...

Installing Python-3.7.0...

BUILD FAILED (Amazon Linux 2 using python-build 1.2.18)

解決方法は、libffi-devをインストールすること。

[root@xxxx dir]# yum install libffi-devel

AWS Lambda + API GatewayでHTTP Post メソッドを呼び出すときにエラー「Missing Authentication Token」~忘れていた初歩的なこと

本当に初歩的なことだが、Googleで検索して出てきたblogなどを参考にしながら、その通りに試していたらハマってしまった(´;ω;`)。
洗脳された感じ?www
ミスリーディングに勝手にハマった感じ?それとも、勝手にハマってミスリーディングだと思っているだけ?www
とにかく、ハマった。気がつけば、初心者的なハマり方で恥ずかしいのだが似ている人もいると思ったし、とあるblogでも「これはおかしい!」と断言している人がいたのでw。

さて、まずは結論(解決策)から。
◎結論(解決策)

API Gatewayで作成したメソッドはPOSTメソッドだったが、アクセスエンドポイントのURLをブラウザから叩くとGET通信となるので、POST通信としてあげる必要がある。

マジで、新人レベルのお話・・・。

◎ハマったこと
AWSのLambda+API Gatewayで作成したサンプルメソッドに対して、AWSがURLを割り当ててくれるのだが、それをブラウザから叩いても次のエラーが表示されてレスポンスが返ってこない。

{"message":"Missing Authentication Token"}

これは403エラーが返却される場合にAWSAPI Gatewayから返却されるエラーメッセージ。

API Gatewayを作成したあとに、AWSが提示してくれたURLを叩いているのに何故エラーとなるのか。。。

当然ながらググるわけだが、どれも「URLが間違っていた」、「URLが間違っている」というものばかり・・・。
「URLは間違っているわけがない!なぜならAPI Gatewayが提示したものだぞ!」と思いながらも、API Gatewayの「リソース」を使用してルートの1つに子階層(子リソース)を作成し、そこに「メソッド」(GET)を新しく作成して試してみた。

そうすると、うまくいく。。。あれなんで?GETとPOSTで違いがある・・・。考えること1分。。。

うはあああああああああああああああああああああああああああああああ。ブラウザのURL叩いてPOST通信呼び出そうとしているじゃん。うはあああああああああああああああああああああああああ。

はい、解決。POST通信してやりましょう
WEBアプリでformにmethod=POSTとかしてやるといいけど、作るの面倒!だからPowerShellInvoke-WebRequest使った。

Invoke-WebRequest -Method Post 'アクセスするURL'
もしくは
curl -Method Post 'アクセスするURL'

これで成功。

以上だけど、以下PowerShellでのcurl補足事項。

◎「curl -Method」って何???
curlって普通はPOST通信するとき次のようなコマンドじゃなかったかな?

curl -XPOST -d 'token=te9aera' https://xxx

これをWindowsPowerShellから実行するとエラー。'XPOST'に一致するパラメーターが見つかりませんと。
Windowscurlcurlじゃないのかと思い、PowerShellでPOST通信する方法を調べると、「Invoke-WebRequest -Method Post 」。

Invoke-WebRequest -Method Post https://xxx.test.ap-northeast-1.amazonaws.

これが正解じゃん?
じゃあ、windowscurlってなんだよ?
それで推測で、-XPOSTを-Method Postに置き換えてみると、それでも動いたというオチ・・・。

Microsoft Power Automate(Microsoft Flow)からTeamsへ投稿する際の注意

相変わらず中途半端な仕様で混乱しか招かないMicrosoft製品(主観と偏見です)。

◎今回のハマりどころ

Flowで表示されるTeamsのコネクタを使用して、FlowからTeamsへ投稿してもリンクが有効にならない

「Flowで表示されるTeamsのコネクタ」とはこんなやつで、メッセージを投稿するってあるじゃないですか。こいつです。
f:id:graySpace:20200306203201p:plain
これを使って投稿しても、リンクが有効になりません。
HTMLだと有効になるかと考えたのですが、それも駄目でした。

それでどうするかというと。。。

Incoming Webhook を使えば、JSON 形式のメッセージを送信することでリッチなテキストを Teams に投稿することができます。

はい。Teamsでwebhookを作成して、Flowからは次のようなHTTPコネクタでPOSTしてやります。。。
f:id:graySpace:20200306203311p:plain

知らんがな。。。どちらもOKにしてくれ・・・。

以下が参考になりました。
qiita.com

Azure Functionsメモ ~ Microsoft.Azure.Storage.FileをVisual Studio Codeでusingするための準備

参考にしている書籍がVisual Studio Codeではなく、Visual Studioを使用しており、VSCodeとは異なるGUIで実施していたためメモ。
まずは公式ドキュメントで、VSCodeからStorageを利用する方法を探すと下記が見つかる。

docs.microsoft.com

これによると下記記載あり。

バインディング拡張機能を登録する
Queue storage の出力バインドを使用しているため、このプロジェクトを実行する前に Storage のバインド拡張機能をインストールしておく必要があります。
HTTP トリガーとタイマー トリガーを除き、バインドは拡張機能パッケージとして実装されます。 ターミナル ウィンドウで次の dotnet add package コマンドを実行して、Storage 拡張機能パッケージをプロジェクトに追加します。

donetを使用するとのことだが、パッケージ名を確認。
www.nuget.org

最新バージョンも確認したところで下記を実行。

dotnet add package Microsoft.Azure.Storage.File --version 11.1.3

これで、C#プログラムで下記のように記載してもエラーとならなくなった。

using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.File;

Azure Functionsメモ ~ host.jsonの役割

Version2.xの情報だけど、公式ドキュメントは下記。
docs.microsoft.com

これによると、host.jsonの役割は下記。

The host.json metadata file contains global configuration options that affect all functions for a function app.
メタデータファイルhost.jsonは、function appの全てのfunctionに影響を及ぼすグローバル設定オプションを含むファイル。

更に、

Other function app configuration options are managed in your app settings (for deployed apps) or your local.settings.json file (for local development).
他のfunction appの設定オプションについては、下記2通りで設定する。
deployed appsについては、app settingsで、local開発に関しては、local.setting.jsonファイルで設定する。

それでサンプルが下記のようであると。

{
    "version": "2.0",
    "aggregator": {
        "batchSize": 1000,
        "flushTimeout": "00:00:30"
    },
    "extensions": {
        "cosmosDb": {},
        "durableTask": {},
        "eventHubs": {},
        "http": {},
        "queues": {},
        "sendGrid": {},
        "serviceBus": {}
    },
    "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[1.*, 2.0.0)"
    },
    "functions": [ "QueueProcessor", "GitHubWebHook" ],
    "functionTimeout": "00:05:00",
    "healthMonitor": {
        "enabled": true,
        "healthCheckInterval": "00:00:10",
        "healthCheckWindow": "00:02:00",
        "healthCheckThreshold": 6,
        "counterThreshold": 0.80
    },
    "logging": {
        "fileLoggingMode": "debugOnly",
        "logLevel": {
          "Function.MyFunction": "Information",
          "default": "None"
        },
        "applicationInsights": {
            "samplingSettings": {
              "isEnabled": true,
              "maxTelemetryItemsPerSecond" : 20,
              "evaluationInterval": "01:00:00",
              "initialSamplingPercentage": 100.0, 
              "samplingPercentageIncreaseTimeout" : "00:00:01",
              "samplingPercentageDecreaseTimeout" : "00:00:01",
              "minSamplingPercentage": 0.1,
              "maxSamplingPercentage": 100.0,
              "movingAverageRatio": 1.0,
              "excludedTypes" : "Dependency;Event",
              "includedTypes" : "PageView;Trace"
            },
            "enableLiveMetrics": true,
            "enableDependencyTracking": true,
            "enablePerformanceCountersCollection": true,            
            "httpAutoCollectionOptions": {
                "enableHttpTriggerExtendedInfoCollection": true,
                "enableW3CDistributedTracing": true,
                "enableResponseHeaderInjection": true
            },
            "snapshotConfiguration": {
                "agentEndpoint": null,
                "captureSnapshotMemoryWeight": 0.5,
                "failedRequestLimit": 3,
                "handleUntrackedExceptions": true,
                "isEnabled": true,
                "isEnabledInDeveloperMode": false,
                "isEnabledWhenProfiling": true,
                "isExceptionSnappointsEnabled": false,
                "isLowPrioritySnapshotUploader": true,
                "maximumCollectionPlanSize": 50,
                "maximumSnapshotsRequired": 3,
                "problemCounterResetInterval": "24:00:00",
                "provideAnonymousTelemetry": true,
                "reconnectInterval": "00:15:00",
                "shadowCopyFolder": null,
                "shareUploaderProcess": true,
                "snapshotInLowPriorityThread": true,
                "snapshotsPerDayLimit": 30,
                "snapshotsPerTenMinutesLimit": 1,
                "tempFolder": null,
                "thresholdForSnapshotting": 1,
                "uploaderProxy": null
            }
        }
    },
    "managedDependency": {
        "enabled": true
    },
    "singleton": {
      "lockPeriod": "00:00:15",
      "listenerLockPeriod": "00:01:00",
      "listenerLockRecoveryPollingInterval": "00:01:00",
      "lockAcquisitionTimeout": "00:01:00",
      "lockAcquisitionPollingInterval": "00:00:03"
    },
    "watchDirectories": [ "Shared", "Test" ]
}

これ全ての説明は公式ドキュメントを参照するとして、1つだけ説明。

functions

A list of functions that the job host runs. An empty array means run all functions. Intended for use only when running locally. In function apps in Azure, you should instead follow the steps in How to disable functions in Azure Functions to disable specific functions rather than using this setting.

job hostが実行する関数リスト。
空配列の場合は全ての関数を実行するという意味である。
ローカルで実行する場合にのみ有効。
Azure上でのfunction appsでは、この設定ではなく、Azure Functionsでの関数の無効化方法に関する説明に従う必要がある。

試しにhost.jsonに次のように2つ記載してみた。

{
    "version": "2.0",
    "functions": ["HttpTriggerExample", "HttpTriggerReference"]
}

この状態でF5キーを押してローカル実行すると、コンソールに次のようにログが出力された。
f:id:graySpace:20200227165354p:plain

host.jsonから1つ関数名を削除して次のようにすると。

{
    "version": "2.0",
    "functions": ["HttpTriggerExample"]
}

たしかに1つだけ実行。
f:id:graySpace:20200227165726p:plain